文件拷贝

本文提供了一个使用Java进行文件复制的简单示例,演示了如何从一个文件读取数据并写入另一个文件的过程。该程序首先检查源文件是否存在,然后通过FileInputStream和FileOutputStream进行字节流操作来完成文件内容的复制。

package com.etc.test;

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

/**
* @deprecated 文件拷贝 前提是文件夹存在
* @author wcc
*
*/
public class CopyFile {
/**
* @param args
*/
public static void main(String[] args) {
CopyFile copyFile = new CopyFile();
File fromFile = new File("E:\\11.txt");
File toFile = new File("D:\\22.txt");
try {
copyFile.copyFile(fromFile, toFile);
} catch (Exception e) {
e.printStackTrace();
}
}

public void copyFile(File fromFile, File toFile) throws Exception {
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try {
if (fromFile.exists()) {
inputStream = new FileInputStream(fromFile);
outputStream = new FileOutputStream(toFile);
byte[] byte1 = new byte[1024];
int n = 0;
while ((n = inputStream.read(byte1)) != -1) {
outputStream.write(byte1, 0, n);
}
} else {
throw new Exception("文件不存在");
}
} catch (FileNotFoundException e) {
throw new FileNotFoundException("文件不存在" + e);
} catch (IOException e) {
throw new IOException("文件不存在" + e);
} finally {
try {
inputStream.close();
outputStream.close();
} catch (IOException e) {
throw new IOException("关闭流错误" + e);
}

}
}

}

转载于:https://www.cnblogs.com/wcc-20170216/p/8450393.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值