使用fileChannel无法正确显示日文字符

时间:2017-06-16 05:37:43

标签: java xml utf-8

我有一个xml文件里面有一些日文字符。但是当我正在阅读文件时,它会被转换为其他一些字符。请参阅下面的代码: -

Customer.java

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Customer {

    @Override
    public String toString() {
        return "Customer [name=" + name + ", age=" + age + ", id=" + id + "]";
    }

    String name;
    int age;
    int id;

    public String getName() {
        return name;
    }

    @XmlElement
    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    @XmlElement
    public void setAge(int age) {
        this.age = age;
    }

    public int getId() {
        return id;
    }

    @XmlAttribute
    public void setId(int id) {
        this.id = id;
    }
}

Conversion.java

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Conversion {
    public static void main(String[] args)
            throws Exception {
        Conversion conversion=new Conversion();
        Path path = Paths.get("C:\\file.xml");
        conversion.doReadFileNew(path);
    }   
    private static void doReadFileNew(Path fileLocation) throws IOException, FileNotFoundException {
         final int READ_FILE_BUFFER_SIZE = Integer
                    .valueOf(System.getProperty("READ_FILE_BUFFER_SIZE", "8192"));
        StringBuilder output = null;
        try (RandomAccessFile raf = new RandomAccessFile(fileLocation.toFile(), "r");
                FileChannel fc = raf.getChannel();) {
                output = new StringBuilder(READ_FILE_BUFFER_SIZE);
                try {
                    ByteBuffer buffer = ByteBuffer.allocate(READ_FILE_BUFFER_SIZE);
                    while (fc.read(buffer) > 0) {
                        buffer.flip();
                        for (int i = 0; i < buffer.limit(); i++) {
                            output.append((char)buffer.get());
                        }
                        buffer.clear();
                    }
                } finally { }
        } catch (Exception e) {
            throw e;
        }
        System.out.println(output);
    }
}

输入文件&#34; file.xml&#34;是: -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="100">
    <age>29</age>
    <name>株式会社三菱東京UFJ銀行</name>
</customer>

OutPut是: -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="100">
    <age>29</age>
    <name>₩ᅠᆰ¥ᄐマ¦ᄐレ￧ᄂᄒ¦ᄌノ│マᄆ₩ンᄆ¦ᄎᆲUFJ←ハタ│ᄀフ</name>
</customer>

请帮忙。

2 个答案:

答案 0 :(得分:0)

您可能需要添加Charset.forName("UTF-8").decode(buffer)

private static void doReadFileNew(Path fileLocation) throws IOException, FileNotFoundException {
     final int READ_FILE_BUFFER_SIZE = Integer
                .valueOf(System.getProperty("READ_FILE_BUFFER_SIZE", "8192"));
    StringBuilder output = null;
    Charset charset = Charset.forName("UTF-8");
    try (RandomAccessFile raf = new RandomAccessFile(fileLocation.toFile(), "r");
            FileChannel fc = raf.getChannel();) {
            output = new StringBuilder(READ_FILE_BUFFER_SIZE);
            try {
                ByteBuffer buffer = ByteBuffer.allocate(READ_FILE_BUFFER_SIZE);
                while (fc.read(buffer) > 0) {
                    buffer.flip();
                    for (int i = 0; i < buffer.limit(); i++) {
                        output.append(charset.decode(buffer));
                    }
                    buffer.clear();
                }
            } finally { }
    } catch (Exception e) {
        throw e;
    }
    System.out.println(output);
}

答案 1 :(得分:0)

试试这个。

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Conversion {
    public static void main(String[] args) throws Exception {
        doReadFileNew("C:\\file.xml");        
    }
    private static void doReadFileNew(String path) throws FileNotFoundException, IOException {
        BufferedReader in = new BufferedReader(new FileReader(path));
        String line;
        while((line = in.readLine()) != null)
        {
            System.out.println(line);
        }
        in.close();
    }   
}
相关问题