package file; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; /** * 在项目指定目录下建一个Java文件,并写入内容 */ public class BufferedWriterTest { public static void main(String[] str) throws IOException { //文件放在src/main/java/ 目录下 命名为aaa.java File file = new File("src/main/java/aaa.java"); //如果文件不存在,创建一个文件 if (!file.exists()) { file.createNewFile(); } FileWriter fw = null; BufferedWriter bw = null; try { fw = new FileWriter(file); String content = "fffff"; bw = new BufferedWriter(fw); bw.write(content); } catch (IOException e) { } finally { bw.close(); fw.close(); } System.out.println("success"); } }