2017-06-16 58 views
0

我有一个xml文件,里面有一些日文字符。但是,当我读的文件是越来越转换为其它characters.Please看到下面的代码: -使用fileChannel无法正确显示日文字符

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); 
    } 
} 

输入文件“file.xml” 是: -

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

输出为: -

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

请帮忙。

+2

在这段代码中,你认为你在指定UTF-8,你为什么这么认为?将“byte”转换为“char”与UTF-8无关。 – Andreas

+0

当你的代码不使用它时,你为什么向我们展示'Customer'类? ---为什么你使用'RandomAccessFile'来简单地从头到尾读取文件?使用'FileReader'或'FileInputStream'。更好的是,使用['Files.readAllBytes()'](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#readAllBytes-java.nio.file。 Path-),然后是'new String(bytes,StandardCharsets.UTF_8)'。 – Andreas

回答

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); 
} 
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(); 
    } 
}