Java println(char),字符转换成中文?
请帮我解决这个问题 A有一个输入文件“Trial.txt”,内容为Thanh-Le 以下是我在尝试读取文件时使用的函数:Java println(char),字符转换成中文?,java,Java,请帮我解决这个问题 A有一个输入文件“Trial.txt”,内容为Thanh-Le 以下是我在尝试读取文件时使用的函数: public char[] importSeq(){ File file = new File("G:\\trial.txt"); char temp_seq[] = new char[100]; try{ FileInputStream fis = new FileInputStream(file); BufferedIn
public char[] importSeq(){
File file = new File("G:\\trial.txt");
char temp_seq[] = new char[100];
try{
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
int i = 0;
//Try to read all character till the end of file
while(dis.available() != 0){
temp_seq[i]=dis.readChar();
i++;
}
System.out.println(" imported");
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return temp_seq;
}
主要功能是:
public static void main(String[] args) {
Sequence s1 = new Sequence();
char result[];
result = s1.importSeq();
int i = 0;
while(result[i] != 0){
System.out.println(result[i]);
i++;
}
}
这就是输出
运行:
因为在Java中,字符由2个字节组成,所以当您使用readChar时,它将读取成对的字母并将它们组合成unicode字符
File file = new File("G:/trial.txt");
char[] content = new char[(int) file.length()];
Reader reader = null;
try {
reader = new FileReader(file);
reader.read(content);
} finally {
if (reader != null) try { reader.close(); } catch (IOException ignore) {}
}
return content;
您可以使用readByte.来避免这种情况。。取而代之的是..因为在Java中,字符由2个字节组成,所以当您使用readChar时,它将读取成对的字母并将它们组合成unicode字符
File file = new File("G:/trial.txt");
char[] content = new char[(int) file.length()];
Reader reader = null;
try {
reader = new FileReader(file);
reader.read(content);
} finally {
if (reader != null) try { reader.close(); } catch (IOException ignore) {}
}
return content;
您可以使用readByte.来避免这种情况。。相反,…老实说,这是一种将文本文件读入char[]的非常笨拙的方法 这里有一个更好的示例,假设文本文件只包含ASCII字符
File file = new File("G:/trial.txt");
char[] content = new char[(int) file.length()];
Reader reader = null;
try {
reader = new FileReader(file);
reader.read(content);
} finally {
if (reader != null) try { reader.close(); } catch (IOException ignore) {}
}
return content;
然后要打印字符[],只需执行以下操作:
System.out.println(content);
请注意,InputStreamavailable不一定能满足您的期望
另见:
老实说,这是一种将文本文件读入char[]的非常笨拙的方法 这里有一个更好的示例,假设文本文件只包含ASCII字符
File file = new File("G:/trial.txt");
char[] content = new char[(int) file.length()];
Reader reader = null;
try {
reader = new FileReader(file);
reader.read(content);
} finally {
if (reader != null) try { reader.close(); } catch (IOException ignore) {}
}
return content;
然后要打印字符[],只需执行以下操作:
System.out.println(content);
请注意,InputStreamavailable不一定能满足您的期望
另见:
一些代码来演示到底发生了什么。Java中的字符由两个字节组成,代表一个字符,即您在屏幕上看到的字形像素。Java中的默认编码是UTF-16,这是使用两个字节表示所有标志符号之一的一种特殊方式。您的文件有一个字节来表示一个字符,可能是ASCII。读取一个UTF-16字符时,从文件中读取两个字节,从而读取两个ASCII字符 下面的代码试图解释单个ASCII字节“t”和“h”如何变成一个中文UTF-16字符
public class Main {
public static void main(String[] args) {
System.out.println((int)'t'); // 116 == x74 (116 is 74 in Hex)
System.out.println((int)'h'); // 104 == x68
System.out.println((int)'瑨'); // 29800 == x7468
// System.out.println('\u0074'); // t
// System.out.println('\u0068'); // h
// System.out.println('\u7468'); // 瑨
char th = (('t' << 8) + 'h'); //x74 x68
System.out.println(th); //瑨 == 29800 == '\u7468'
}
}
一些代码来演示到底发生了什么。Java中的字符由两个字节组成,代表一个字符,即您在屏幕上看到的字形像素。Java中的默认编码是UTF-16,这是使用两个字节表示所有标志符号之一的一种特殊方式。您的文件有一个字节来表示一个字符,可能是ASCII。读取一个UTF-16字符时,从文件中读取两个字节,从而读取两个ASCII字符 下面的代码试图解释单个ASCII字节“t”和“h”如何变成一个中文UTF-16字符
public class Main {
public static void main(String[] args) {
System.out.println((int)'t'); // 116 == x74 (116 is 74 in Hex)
System.out.println((int)'h'); // 104 == x68
System.out.println((int)'瑨'); // 29800 == x7468
// System.out.println('\u0074'); // t
// System.out.println('\u0068'); // h
// System.out.println('\u7468'); // 瑨
char th = (('t' << 8) + 'h'); //x74 x68
System.out.println(th); //瑨 == 29800 == '\u7468'
}
}
您好,谢谢您的回复,当我试图将其修改为readByte时,不可编译的源代码-可能会丢失精度。这有什么办法吗?另外,谢谢,当我将变量类型更改为Byte[]时,它打印的是数字而不是中文,这是因为字节不是字符。为了正确读取字符,您需要一个FileReader或InputStreamReader,它使用创建对象时指定的编码,或者通常默认为UTF-8来将字节转换为字符。您好,感谢您的响应,当我试图将其修改为readByte时,不可编译的源代码-可能会丢失精度。这有什么办法吗?另外,谢谢,当我将变量类型更改为Byte[]时,它打印的是数字而不是中文,这是因为字节不是字符。为了正确读取字符,您需要一个FileReader或InputStreamReader,它使用创建对象时指定的编码,或者通常默认为UTF-8来将字节转换为字符。它可以工作!!!!非常感谢。是的,它很笨拙。。。我是编程新手。。。但是谢谢你:不客气。在未来,试着阅读正确的教程/书籍,而不是在黑暗中瞎逛:这很有效!!!!非常感谢。是的,它很笨拙。。。我是编程新手。。。但是谢谢你:不客气。今后,尝试阅读正确的教程/书籍,而不是在黑暗中瞎逛: