PrintWriter追加至文本文件
来源:互联网 发布:php 腾讯云直播demo 编辑:程序博客网 时间:2024/05/17 02:47
- import java.util.*;
- import java.io.*;
- public class T {
- public static void main(String[] args) {
- String fileName = "data.txt";
- FileWriter fw = null;
- PrintWriter toFile = null;
- try {
- fw = new FileWriter(fileName, true);
- toFile = new PrintWriter(fw);
- } catch (FileNotFoundException e) {
- System.out.println("PrintWriter error opening the file " + fileName);
- System.exit(0);
- } catch (IOException e) {
- System.out.println("FileWriter error opening the file " + fileName);
- System.exit(0);
- }
- System.out.println("Enter four lines of text:");
- Scanner keyboard = new Scanner(System.in);
- for (int count = 1; count <= 4; count++) {
- String line = keyboard.nextLine();
- toFile.println(count + " " + line);
- }
- System.out.println("Four lines were written to " + fileName);
- toFile.close();
- }
- }