当前位置: 代码迷 >> 综合 >> 建一个文件“note.txt“,然后通过控制台输入的每一行字符串都按行写入到 * note.txt中。当输入的字符串为“exit“时退出程序。
  详细解决方案

建一个文件“note.txt“,然后通过控制台输入的每一行字符串都按行写入到 * note.txt中。当输入的字符串为“exit“时退出程序。

热度:85   发布时间:2023-12-02 06:00:01.0
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();}
}

  相关解决方案