Java 使用fileChannel无法正确显示日语字符
我有一个xml文件,里面有一些日语字符。但当我读取文件时,它会被转换为其他字符。请参阅下面的代码:- Customer.javaJava 使用fileChannel无法正确显示日语字符,java,xml,utf-8,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 {
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);
}
}
import java.io.FileNotFoundException;
导入java.io.IOException;
导入java.io.RandomAccessFile;
导入java.nio.ByteBuffer;
导入java.nio.channels.FileChannel;
导入java.nio.file.Path;
导入java.nio.file.path;
公共类转换{
公共静态void main(字符串[]args)
抛出异常{
转换=新转换();
Path Path=Path.get(“C:\\file.xml”);
转换.doReadFileNew(路径);
}
私有静态void doReadFileNew(路径fileLocation)引发IOException、FileNotFoundException{
最终整数读取文件缓冲区大小=整数
.valueOf(System.getProperty(“读取文件缓冲区大小”,“8192”);
StringBuilder输出=null;
try(RandomAccessFile raf=newrandomAccessFile(fileLocation.toFile(),“r”);
FileChannel fc=raf.getChannel();){
输出=新的StringBuilder(读取文件缓冲区大小);
试一试{
ByteBuffer buffer=ByteBuffer.allocate(读取文件缓冲区大小);
而(fc.读取(缓冲区)>0){
flip();
对于(int i=0;i
输入文件“file.xml”是:-
29
株式会社三菱東京UFJ銀行
输出为:-
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="100">
<age>29</age>
<name>₩ᅠᆰ¥ᄐマ¦ᄐレᄂᄒ¦ᄌノ│マᄆ₩ンᄆ¦ᄎᆲUFJ←ハタ│ᄀフ</name>
</customer>
29
₩ᅠᆰ¥ᄐマ¦ᄐレᄂᄒ¦ᄌノ│マᄆ₩ンᄆ¦ᄎᆲUFJ←ハタ│ᄀフ
请提供帮助。您可能需要添加
字符集.forName(“UTF-8”)。解码(缓冲区)
:
private static void doReadFileNew(路径fileLocation)抛出IOException、FileNotFoundException{
最终整数读取文件缓冲区大小=整数
.valueOf(System.getProperty(“读取文件缓冲区大小”,“8192”);
StringBuilder输出=null;
Charset Charset=Charset.forName(“UTF-8”);
try(RandomAccessFile raf=newrandomAccessFile(fileLocation.toFile(),“r”);
FileChannel fc=raf.getChannel();){
输出=新的StringBuilder(读取文件缓冲区大小);
试一试{
ByteBuffer buffer=ByteBuffer.allocate(读取文件缓冲区大小);
而(fc.读取(缓冲区)>0){
flip();
对于(int i=0;i
试试这个
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();
}
}
在这段代码中,您认为您在哪里指定UTF-8,为什么您这么认为?将
字节
强制转换为字符
与UTF-8无关。当您的代码不使用它时,为什么向我们显示classCustomer
为什么要使用RandomAccessFile
从头到尾简单地读取文件?使用文件读取器
或文件输入流
。更好的方法是使用新字符串(bytes,StandardCharsets.UTF_8)
。
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);
}
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();
}
}