java(IO)读写文件乱码转换UTF-8问题
读取文件 String Content = ""; // 文件很长的话建议使用StringBuffer try { 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(); } }