输入时可输入copy,体现出文件的复制

package dan;

import java.io.*;
import java.util.*;

public class kus {
    static final String input="d:/student.txt";//定义输入文件路径
    static final String output="d:/student.txt";//定义输出文件路径

    public static void main(String[] args) throws FileNotFoundException{
        // TODO 自动生成的方法存根
        int ir;
        String str;
        RandomAccessFile rd=new RandomAccessFile(input,"rw");//创建具有读/写功能RandomAccessFile对象rd
        FileInputStream fis=new FileInputStream(input);//创建 文件读入流对象fis
        FileOutputStream fos=new FileOutputStream(output);//创建文件写出流对象fos
        try{
            System.out.println("添加文件内容");//从键盘上输入信息
            Scanner sc=new Scanner(System.in);
            str=sc.next();
            rd.writeBytes(str);//将读入的字符串信息写入RandomAccessFile对象rd
            System.out.println("开始复制文件"+input);
            do{//将对象fis中的内容写入对象fos,即实现文件内容复制的功能,到文件结尾ir=-1为止
                ir=fis.read();
                if(ir!=-1){
                    fos.write(ir);
                    System.out.println("...\n");
                }

            }while(ir!=-1);
                System.out.println(input+"已成功复制到"+output);
                fis.close();//关闭对象fis和对象fos
                fos.close();
        }

        catch(IOException e){
            e.printStackTrace();
        }

    }

}

运行结果:
这里写图片描述

知识点:
1.字节流:字节流分为输入和输出.输入流是指能够读出一系列字节的对象. 输出流是指能够写入一系列的对象.
2.Input Stream类:字节流的抽象类
3.Output Stream 类:字节流的抽象类
4.File Input Stream 类: 如果要将数据源文件读入程序,则需要.File Input Stream实现.
5.File Output Stream 类: 如果要将程序内部的数据源文件输出程序,则需要.File Input Stream实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值