java(IO)读写文件乱码转换UTF-8问题
生活随笔
收集整理的這篇文章主要介紹了
java(IO)读写文件乱码转换UTF-8问题
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
java(IO)讀寫文件亂碼轉(zhuǎn)換UTF-8問題
讀取文件String?Content?=?"";?//?文件很長的話建議使用StringBuffertry?{FileInputStream fs=new FileInputStream("文件錄取");InputStreamReader?isr?=?new?InputStreamReader(fis,?"UTF-8");BufferedReader?br?=?new?BufferedReader(isr);String?line?=?null;while?((line?=?br.readLine())?!=?null)?{Content?+=?line;Content?+=?"\r\n";?//?補上換行符}catch?(Exception?e)?{?????????e.printStackTrace();} 寫入文件 public static void outFile(String sb) {try {File file = new File("G:\\liuhecai.txt");if (file.exists()) {file.delete();}file.createNewFile();FileOutputStream fos = new FileOutputStream(file);OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");osw.write(sb);} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}?
?
轉(zhuǎn)載于:https://www.cnblogs.com/weibanggang/p/9347778.html
總結(jié)
以上是生活随笔為你收集整理的java(IO)读写文件乱码转换UTF-8问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rds具备哪些安全策略?
- 下一篇: 2018牛客暑假多校二 D(贪心)