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

package homework007;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class Text001
{

    public static void main(String[] args) throws Exception
    {
        
        try
        {
        
            //读取
            FileInputStream in = new FileInputStream("e:/text.txt");
            String str = "";
            int i =0;            
            byte[] b1 = new byte[1024];
            while((i=in.read(b1))>0)
            {
                str+=new String(b1,0,i);
            }            
            in.close();
            System.out.println("文件读取完成");
            
            //写入
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入您要另存的文件全路径");
            
            String str1 = sc.nextLine();
            FileOutputStream out = new FileOutputStream(str1);
            byte[] b = str.getBytes();
            out.write(b);
            System.out.println("内容添加成功");
            out.close();
        } 
        catch (FileNotFoundException e)
        {        
            e.printStackTrace();
        }
        
    }

}

原文地址:https://www.cnblogs.com/HRZJ/p/5914409.html