PrintWriter写文本文件
来源:互联网 发布:php 腾讯云直播demo 编辑:程序博客网 时间:2024/05/22 06:21
- 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);
- } 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);
- } 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();
- }
- }