1 package file; 2 3 import java.io.File; 4 import java.io.FileWriter; 5 import java.io.IOException; 6 7 public class Demo6 { 8 public static void main(String[] args) throws IOException { 9 writeTest1(); 10 } 11 12 public static void writeTest1() throws IOException { 13 File file = new File("F:\2.txt"); 14 FileWriter fileWriter = new FileWriter(file); 15 // FileWriter fileWriter = new FileWriter(file,true);//如果追加,使用FileWriter(file,true) 16 //准备数据,写出 17 String data = "测试123数据"; 18 fileWriter.write(data); 19 //关闭资源 20 // fileWriter.flush(); 21 fileWriter.close(); 22 23 } 24 }