public class Test09 {
public static void main(String[] args) throws Exception {
File file = new File("E:\\upload\\note.txt");
if (!file.exists()) {
file.createNewFile();
System.out.println("文件创建成功");
}
BufferedWriter bw =null;
bw = new BufferedWriter(new FileWriter("E:\\upload\\note.txt"));
Scanner sc = new Scanner(System.in);
System.out.println("请输入一行字符串:");
String str=sc.nextLine();
System.out.println(str);
bw.write(str);
System.out.println("写入成功");
bw.close();
}
}
建一个文件“note.txt“,然后通过控制台输入的每一行字符串都按行写入到 * note.txt中。当输入的字符串为“exit“时退出程序。
最新推荐文章于 2022-01-29 10:20:09 发布