package com.jxj;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class FileCopy {
/**
* main函数测试
*/
public static void main(String[] args) {
filesCopy();
}
/**
* 将文件夹oldPath里的文件复制到另外一个文件夹newPath里. 注意:oldPath文件夹下只包含文件,不包含文件夹
*/
public static void filesCopy() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入所要复制的文件夹的路径:");
String oldPath = sc.nextLine();
System.out.println("请输入目标文件夹的路径:");
String newPath = sc.nextLine();
File oldDirectory = new File(oldPath);
File newDirectory = new File(newPath);
// 判断目标文件夹是否存在,如果不存在就创建文件夹
if (!newDirectory.exists())
// boolean mkdirs() 创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
newDirectory.mkdirs();
File[] oldFile = null;
FileInputStream fielInputStream = null;
FileOutputStream fileOutputStream = null;
try {
// File[] listFiles():返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。
oldFile = oldDirectory.listFiles();
for (int i = 0; i < oldFile.length; i++) {
// 获取文件名字
String fileName = oldFile[i].getName();
File newFile = new File(newDirectory + File.separator + fileName);
// 在新文件下创建同名文件
if (!newFile.exists())
newFile.createNewFile();
fielInputStream = new FileInputStream(oldFile[i]);
fileOutputStream = new FileOutputStream(newFile);
byte[] buffer = new byte[1024];
int length;
while ((length = fielInputStream.read(buffer)) != -1)
// 写入文件
fileOutputStream.write(buffer, 0, length);
}
fileOutputStream.close();
fielInputStream.close();
System.out.println("复制完成!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class FileCopy {
/**
* main函数测试
*/
public static void main(String[] args) {
filesCopy();
}
/**
* 将文件夹oldPath里的文件复制到另外一个文件夹newPath里. 注意:oldPath文件夹下只包含文件,不包含文件夹
*/
public static void filesCopy() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入所要复制的文件夹的路径:");
String oldPath = sc.nextLine();
System.out.println("请输入目标文件夹的路径:");
String newPath = sc.nextLine();
File oldDirectory = new File(oldPath);
File newDirectory = new File(newPath);
// 判断目标文件夹是否存在,如果不存在就创建文件夹
if (!newDirectory.exists())
// boolean mkdirs() 创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
newDirectory.mkdirs();
File[] oldFile = null;
FileInputStream fielInputStream = null;
FileOutputStream fileOutputStream = null;
try {
// File[] listFiles():返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。
oldFile = oldDirectory.listFiles();
for (int i = 0; i < oldFile.length; i++) {
// 获取文件名字
String fileName = oldFile[i].getName();
File newFile = new File(newDirectory + File.separator + fileName);
// 在新文件下创建同名文件
if (!newFile.exists())
newFile.createNewFile();
fielInputStream = new FileInputStream(oldFile[i]);
fileOutputStream = new FileOutputStream(newFile);
byte[] buffer = new byte[1024];
int length;
while ((length = fielInputStream.read(buffer)) != -1)
// 写入文件
fileOutputStream.write(buffer, 0, length);
}
fileOutputStream.close();
fielInputStream.close();
System.out.println("复制完成!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}