编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt

package com.hanqi.io;

import java.io.*;


public class IoDemo {

    public static void main(String[] args) {
        try 
        {
            File file=new File("d:/testRW.txt");
            if(!file.exists())
            {            
            file.createNewFile();
             }
            
        
         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("d:/testRW.txt");
         System.out.println(str);
        
        File file2=new File("d:/iodemo.txt");
        if(!file2.exists())
        {            
           file2.createNewFile();
        }
        FileWriter fw=new FileWriter(file2);
         fw.write(str);
         fw.close();

         FileReader    fr2 = new FileReader(file2);
        
         String str2="";
         char[] a2=new char[1024];
         int j=0;
         while((j=fr2.read(a2))>0)
         {
             str2+=new String(a2,0,j);
         }
         
         fr2.close();
         System.out.println("d:/iodemo.txt");
         System.out.println(str2);
        }
        catch (Exception e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }

}

原文地址:https://www.cnblogs.com/wenwen123/p/5546196.html