编写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:/testRW.txt");
             if(!file.exists())
             {            
                file.createNewFile();
             }    
             FileWriter fw=new FileWriter(file);
             fw.write("学号:*********\n");
             fw.write("姓名:***");
             fw.close();
             FileReader fr=new FileReader(file);
             String str="";
             char[] a=new char[1024];
             int i=0;
             while((i=fr.read(a))>0)
             {
                 str+=new String(a,0,i);
             }
             
             fr.close();
             System.out.println(str);
            } 
        catch (IOException e) 
        {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
        }

    }

}

posted @ 2016-05-31 15:49  凌零聆  阅读(203)  评论(0编辑  收藏  举报