单字节拷贝:
class CopyUtil {
private CopyUtil() {} //构造私有化
//1.检测源文件目录是否存在
public static boolean fileExists(String path) {
return new File(path).exists(); //文件路径存在返回true
}
//2.如果目标目录不存在,创建父目录
public static void createParentDirectory(String path) {
File file = new File(path);
if(!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
}
//3.创建IO流并调用拷贝方法
public static boolean copy(String src,String des) throws IOException {
boolean flag = true; //状态提示
File inFile = new File(src); //源文件路径
File outFile = new File(des); //目标文件路径
InputStream input = null; //输入流
OutputStream output = null; //输出流
try {
input = new FileInputStream(inFile); //输入流对象
output = new FileOutputStream(des); //输出流对象
copyHandle(input,output); //文件拷贝处理
} catch(Exception e) {
flag = false; //出现异常转换状态和finally一起抛出
} finally {
input.close(); //无论程序是否正常执行,最后关闭流
output.close();
}
return flag;
}
//4.文件拷贝处理
private static void copyHandle(InputStream input,OutputStream output) throws IOException {
int temp; //单个字节数据
while((temp = input.read()) != -1) {//读取源文件信息直到数据末尾
output.write(temp);//写入目标文件信息
}
}
}
public class Copy {
public static void main(String[] args) {
//如果不是源文件目录和目标目录,系统结束
if(args.length != 2) {
System.out.println("错误执行方式");
System.exit(1);
}
if(CopyUtil.fileExists(args[0])) { //检测源文件是否存在
CopyUtil.createParentDirectory(args[1]);
try {
System.out.println(CopyUtil.copy(args[0],args[1]) ? "拷贝成功" : "拷贝失败");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("源文件不存在");
}
}
}
多字节拷贝
class CopyUtil {
private CopyUtil() {} //构造私有化
//1.检测源文件目录是否存在
public static boolean fileExists(String path) {
return new File(path).exists(); //文件路径存在返回true
}
//2.如果源文件目录不存在,创建父目录
public static void createParentDirectory(String path) {
File file = new File(path);
if(!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
}
//3.创建IO流并调用拷贝方法
public static boolean copy(String src,String des) throws IOException {
boolean flag = true; //状态提示
File inFile = new File(src); //源文件路径
File outFile = new File(des); //目标文件路径
InputStream input = null; //输入流
OutputStream output = null; //输出流
try {
input = new FileInputStream(inFile); //输入流对象
output = new FileOutputStream(des); //输出流对象
copyHandle(input,output); //文件拷贝处理
} catch(Exception e) {
flag = false; //出现异常转换状态和finally一起抛出
} finally {
input.close(); //无论程序是否正常执行,最后关闭流
output.close();
}
return flag;
}
//4.文件拷贝处理
private static void copyHandle(InputStream input,OutputStream output) throws IOException {
int temp; //多个字节数据
byte[] bt = new byte[1024];
input.read(bt); //添加1024个数组元素
while((temp = input.read(bt)) != -1) { //存在数组元素时执行写入工作
output.write(bt,0,temp); //读取当前数组所有元素
}
}
}
public class Copy {
public static void main(String[] args) {
//如果不是源文件目录和目标目录,系统结束
if(args.length != 2) {
System.out.println("错误执行方式");
System.exit(1);
}
if(CopyUtil.fileExists(args[0])) { //检测源文件是否存在
CopyUtil.createParentDirectory(args[1]);
try {
System.out.println(CopyUtil.copy(args[0],args[1]) ? "拷贝成功" : "拷贝失败");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("源文件不存在");
}
}
}