public static void createFileAndWriter(String message) { File file = new File("F:\\CreateDemo\\"); if (!file.exists()) { file.mkdir();//创建一个新目录 } FileWriter fileWriter=null; BufferedWriter writer = null; try { //当该目录下不存在demo.txt文件时,会自动创建 fileWriter=new FileWriter("F:\\CreateDemo\\demo.txt"); //创建一个缓冲区,加快写入速度 writer = new BufferedWriter(fileWriter); writer.write(message); writer.close(); fileWriter.close(); } catch (IOException e) { System.out.println("写入信息发生错误!"); e.printStackTrace(); }}
原文地址:https://www.cnblogs.com/czgxxwz/p/10286680.html
时间: 2024-10-09 21:18:00