<span style="background-color: rgb(255, 255, 255);"> <span style="font-size:18px;"><strong>程序功能概述:</strong><span style="font-size:14px;">最近在学校档案馆找了一份兼职,主要的工作就是帮他们上传资料,第一天丧尸的馆长给了我三个文件夹每个文件夹下面有一千多个子文件夹,每个子文件夹为一个主题,代表一个主题的子文件下面有几个网页或文档。在传的时候有的需要传............这就不再赘述了。程序功能就是:这里有一个文件路径a,有一个文件路径b,有一个文本文件c.txt。需要把c.txt里边有的主题在文件路径a(肯定有)下面找出来放到空路径b下边。</span></span></span>
程序代码介绍:共三个源文件:com.john.file.Copyfile.java
com.john.file.Readtxt.java
com.john.file.Main.java
Copyfile.java:实现将一个文件夹下面的所有文件复制到另外一个文件下面
Readtxt.java:实现将规范的文本中存放的文件夹名字读出来
Main.java:主函数所在class
代码:
//Copyfile.java
package com.john.file;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class Copyfile {
public void copyFolder(String oldPath, String newPath) {
try {
(new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
File a = new File(oldPath);
String[] file = a.list();
File temp = null;
for (int i = 0; i < file.length; i++) {
if (oldPath.endsWith(File.separator)) {
temp = new File(oldPath + file[i]);
} else {
temp = new File(oldPath + File.separator + file[i]);
}
if (temp.isFile()) {
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath
+ "/" + (temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if (temp.isDirectory()) {// 如果是子文件夹
copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
}
}
} catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
}
public static void main(String[] args) {
Copyfile test = new Copyfile();
test.copyFolder("E:\\from", "F:\\to");
}
}
//Readtxt.java
package com.john.file;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Readtxt {
public ArrayList<String> read(String path){
FileInputStream fr;
String name;
ArrayList<String> names = new ArrayList<String>();
try {
fr = new FileInputStream(new File(path));
BufferedReader br = new BufferedReader(new InputStreamReader(fr,"utf-8"));
if(br.ready()){
while((name = br.readLine()) != null){
names.add(name);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return names;
}
}
package com.john.file;
//Main.java
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("please input from path:");
String frompath = scanner.nextLine();
System.out.println("please input to path");
String topath = scanner.nextLine();
System.out.println("please input the txtpath:");
String txtpath = scanner.nextLine();
ArrayList<String> names = new Readtxt().read(txtpath);
for(int i=0;i<names.size();i++){
new Copyfile().copyFolder(frompath+"\\"+names.get(i),topath+'\\'+names.get(i));
System.out.println(names.get(i));
}
}
}