面向对象程序设计(Java)实验6

实验要求

  1. 在C盘根目录创建文本文件Hello.txt,并往里写入若干行文本。从Hello.txt中读取文本并显示在屏幕上。

实验内容

package test6;
import java.io.*;
import java.util.Scanner;
public class hello {
    
    
    public static void main(String [] args) throws IOException{
    
    
        try {
    
    
            FileReader fr = new FileReader("D:\\作业\\JAVA学习\\JAVA实验报告\\Hello.txt");
            FileWriter fr2 = new FileWriter("D:\\作业\\JAVA学习\\JAVA实验报告\\Hello.txt");
            BufferedReader fw = new BufferedReader(fr);
            BufferedWriter fw2 = new BufferedWriter(fr2);
            int line;
            String str;
            Scanner input = new Scanner(System.in);
            System.out.println("你要输入文本的行数为:");
            line = input.nextInt();
            for(int i=0;i<line;i++){
    
    
                str = input.next();
                fw2.write(str);
                if(i!=line-1) {
    
    
                    fw2.newLine();
                }
            }
            fw2.close();
            str = fw.readLine();
            while(str!=null){
    
    
                System.out.println(str);
                str = fw.readLine();
            }
            fw.close();
        }
        catch (IOException ioe){
    
    
            System.out.println("Error : " + ioe);
        }
    }
}

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_51594676/article/details/124353779