//读文件 public static String ReadFile(String path) { File file = new File(path); BufferedReader reader = null; String laststr = ""; try { reader = new BufferedReader(new FileReader(file)); String tempString = null; while ((tempString = reader.readLine()) != null) { laststr += tempString; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } return laststr; } //写文件 public static void writeFile(String path,String fileContent) { try { File filename=new File(path); OutputStreamWriter write = new OutputStreamWriter( new FileOutputStream(filename), "UTF-8"); BufferedWriter writer = new BufferedWriter(write); writer.write(fileContent); writer.close(); } catch (Exception e) { System.out.println("写文件内容操作出错"); e.printStackTrace(); } }
java读写文件(可当工具类保存。解决乱码)
时间: 2024-10-25 20:16:16