- import java.util.*;
- import java.io.*;
- public class T {
- public static void main(String[] args) {
- String fileName = "data.txt";
- PrintWriter toFile = null;
- try {
- toFile = new PrintWriter(fileName); // FileNotFoundException
- } catch (FileNotFoundException e) {
- System.out
- .println("PrintWriter error opening the file " + fileName);
- System.exit(0);
- }
- System.out.println("Enter four lines of text:");
- Scanner keyboard = new Scanner(System.in);
- import java.util.*;
- import java.io.*;
- public class T {
- public static void main(String[] args) {
- String fileName = "data.txt";
- PrintWriter toFile = null;
- try {
- toFile = new PrintWriter(fileName); // throw FileNotFoundException
- } catch (FileNotFoundException e) {
- System.out.println("PrintWriter 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();
- }
- }
- 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();
- }
- }
PrintWriter写文本文件
最新推荐文章于 2023-03-28 15:33:16 发布