输入输出练习1

1.编写TextRw.java的Java应用程序,程序完成的功能是:首先向TextRw.txt中写入自己的学号和姓名,读取TextRw.txt中信息并将其显示在屏幕上。

package com.hanqi.io;

import java.io.*;

public class TextRw {

    public static void main(String[] args) {
            
            
        try {
            File file=new File("d:/TextRw.txt");
            
            if(!file.exists())
            {
                file.createNewFile();
            }
            
            //首先向TextRw.txt中写入自己的学号和姓名,
            FileWriter fw=new FileWriter(file);
                                
            fw.write("200913012");
            fw.write("张三");
            
            fw.close();
            
            //读取TextRw.txt中信息并将其显示在屏幕上。
            FileReader fr=new FileReader(file);
            
            char[] c=new char[1024];
            String str="";
            
            int i;
            while((i=fr.read(c))>0)
            {
                str+=new String(c,0,i);
                
            }
            
            fr.close();
            System.out.println("TextRw.txt里的内容是:"+str);
            System.out.println("学号是"+str.substring(0, 9)+",姓名是"+str.substring(9));
            
            
        } catch (Exception e) {
            
            e.printStackTrace();
        }
        
        
        
        
        
        
        
        
        
        
        
        
        
        

    }

}

运行结果:

原文地址:https://www.cnblogs.com/miss123/p/5548346.html