在JAVA中读取文件乱码的解决办法

public static String encodin = "UTF8"
public static String encodout = "UTF8"
static void writeOutput(String str) {
try {
FileOutputStream fos = new FileOutputStream("test.txt");
Writer out = new OutputStreamWriter(fos, encodout);
out.write(str);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
static String readInput() {
StringBuffer buffer = new StringBuffer();
try {
FileInputStream fis = new FileInputStream("test.txt");
InputStreamReader isr = new InputStreamReader(fis, encodin);
Reader in = new BufferedReader(isr);
int ch;
while ((ch = in.read()) > -1) {
buffer.append((char)ch);
}
in.close();
return buffer.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

你可能感兴趣的:(读取文件)