`

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

    博客分类:
  • J2SE
阅读更多
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; 
} 
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics