如何阻止我的输出文件包含中文字符?
How to stop my output file from having Chinese characters?
我正在尝试通过从特定 jtable 的单元格中获取数据来写入 RandomAccessFile。我将字符串转换为字节,然后实现 .write() 函数以写入输出文件。
我这样做是为了能够通过将字节打印到控制台来查看我的输出应该是什么。但是,当我检查我的输出文件时,我看到了汉字。
String data = (String) table.getModel().getValueAt(r,c).toString();
byte[] BytedString = data.getBytes();
file.write(BytedString);
String s = new String(BytedString);
System.out.print(s+" ");
我是不是做错了什么...?
在您的代码中而不是使用:
String s = new String(BytedString);
使用:
String s = new String(BytedString, "UTF-8");
或
new String(BytedString, "US-ASCII");
new String(BytedString, "ISO-8859-1");
取决于您的平台编码。
String data = (String) table.getModel().getValueAt(r,c).toString();
byte[] BytedString = data.getBytes();
String s = new String(BytedString, "UTF-8");
file.writeChars(s);
System.out.print(s+" ");
我正在尝试通过从特定 jtable 的单元格中获取数据来写入 RandomAccessFile。我将字符串转换为字节,然后实现 .write() 函数以写入输出文件。
我这样做是为了能够通过将字节打印到控制台来查看我的输出应该是什么。但是,当我检查我的输出文件时,我看到了汉字。
String data = (String) table.getModel().getValueAt(r,c).toString();
byte[] BytedString = data.getBytes();
file.write(BytedString);
String s = new String(BytedString);
System.out.print(s+" ");
我是不是做错了什么...?
在您的代码中而不是使用:
String s = new String(BytedString);
使用:
String s = new String(BytedString, "UTF-8");
或
new String(BytedString, "US-ASCII");
new String(BytedString, "ISO-8859-1");
取决于您的平台编码。
String data = (String) table.getModel().getValueAt(r,c).toString();
byte[] BytedString = data.getBytes();
String s = new String(BytedString, "UTF-8");
file.writeChars(s);
System.out.print(s+" ");