简单I/O范例

package FileTest.Stream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;

/**
 * Created by Mr.Wang on 2019/2/19.
 */
public class CopyFileTest {
    public static void main(String[] args) throws Exception{
        copyFile("I:\\StudySource\\text.txt","I:\\StudySource\\text2.txt");
    }

    public static void copyFile(String srcFile, String destFile) throws Exception{
        File file1 = new File(srcFile);         //源文件
        File file2 = new File(destFile);        //目标文件

        if (!file1.exists()){                   //判断源文件是否存在
            throw new Exception("源文件不存在");
        }
        if(!file2.exists()){                    //判断目标文件是否存在
            file2.createNewFile();
        }
        FileInputStream fileInputStream = new FileInputStream(srcFile);         //文件输入流
        FileOutputStream fileOutputStream = new FileOutputStream(destFile);     //文件输出流

        byte[] bytes = new byte[8*1024];                                        //单词读入的字节数组
        int end = bytes.length;                                                 
        while ((end = fileInputStream.read(bytes,0,end)) != -1){            //fileInputStream.read(bytes,0,end) 输入流将多个字节读入到数组中,返回读入的字节数
            fileOutputStream.write(bytes, 0, end);                          //输出流将数组中的字节读出到目标文件
        }

        fileInputStream.close();                                                //关闭流
        fileOutputStream.close();           
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值