[b][color=green]最近做了一个对于引入的txt文本进行分割时,老出现乱码,多次修改后,才知道是编码的问题,重新设置编码后,文本文件转字符串就没再出现乱码了。[/color][/b]
public static void writeOutput(String str, File file) {
try {
FileOutputStream fos = new FileOutputStream(file);
Writer out = new OutputStreamWriter(fos, "GBK");
out.write(str);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String readInput(File file) {
StringBuffer buffer = new StringBuffer();
try {
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "GBK");
Reader in = new BufferedReader(isr);
int i;
while ((i= in.read()) > -1) {
buffer.append((char) i);
}
in.close();
return buffer.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}