本帖最后由 122754304 于 2016-4-28 21:05 编辑
package test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class T3 {
/**键盘录入源文件路径,和目标文件路径,将源文件的内容拷贝到目标文件夹
*
*
*/
public static void main(String[] args) throws IOException {
System.out.println("请输入源路径!!!");
File file = getFile();
System.out.println("请输入目标路径!!!");
File file1 = getFile();
kaobei(file,file1);
}
private static void kaobei(File file, File file1) throws IOException {
if(file.isFile() && file1.isFile()){
io(file, file1,null);
}else if(file.isFile() && file1.isDirectory()){
io(file,file1,file.getName());
}else if(file.isDirectory() && file1.isDirectory()){
File[] arr = file.listFiles();
for (File file2 :arr) {
if(file2.isFile()){
io(file2,file1,file2.getName());
}
}
}else{
System.out.println("非法操作");
}
}
private static void io(File file, File file1,String name) throws FileNotFoundException,IOException {
BufferedInputStream bi = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bo = null;
if(name == null){
bo = new BufferedOutputStream(new FileOutputStream(file1));
}else{
bo = new BufferedOutputStream(new FileOutputStream(new File(file1,name)));
}
int b;
while(( b = bi.read()) != -1){
bo.write(b);
}
bi.close();
bo.close();
}
public static File getFile() throws IOException{
while(true){
File file = new File(getString());
if(!file.exists()){
System.out.println("不存在的路径!!!");
}else if(file.isDirectory()){
System.out.println("这是一个目录");
System.out.println("确定拷贝(从,到)这个文件夹吗?\r\n确定:y 不确定:n");
if(getString().equals("y")){
return file;
}
}else if(file.isFile()){
System.out.println("这是一个文件");
System.out.println("确定拷贝(从,到)这个文件吗?\r\n确定:y 不确定:n");
if(getString().equals("y")){
return file;
}
}
}
}
public static String getString(){
Scanner sc = new Scanner(System.in);
return sc.nextLine();
}
}
|
|