文件夹复制,通过配置文件来更改条件
2015-03-01 23:44
204 查看
java-文件夹复制,通过配置文件来更改条件
功能:复制文件夹要求:1、复制文件夹,及路径下所有文件到指定的路径。
2、选择性复制,对于路径下所有的文件,可以选择性的复制,指定的文件类型就不复制。
3、通过配置文件来修改不需要的文件类型,源目录和目的目录的路径。
4、配置文件示意图
思路:1、写一个复制文件的方法。
2、写一个文件遍历的方法。
3、在文件复制的时候,判断是不是药复制的文件,那么就需要一个过滤器。所以还有写一个过滤方法。
4、由于我们需要过滤的文件类型后缀全部在配置文件中,所以我们还需要定义一个方法,来判断当前文件类型是不是在配置文件中。
5、既然上面都说需要读取配置文件,那么我们就需要通过流来读取配置文件的内容。然后将读取的文件,返回成字符串。
说明:1、整个程序分别在两个文件中,所以到时候要注意是两个文件。主文件是:CopyDir.java。子文件是:RecognitionFileType.java。
2、我的程序使用的JDK版本1.7版本。编译器eclipse,使用系统是windows 7 -64bit。包名:copyfileall
3、程序中,很多程序都只是注释掉了,但是没有删除。因为我觉得也许对你们帮助,所以有些我就没有删除掉。
代码部分:
第一部分(注:这部分的代码全部是在一个.java文件中,文件名:CopyDir.java)
package copyfileall;
/*
* 实现复制目录
*/
import java.io.*;
import sun.font.CreatedFontTracker;
/*
* 功能:实现过滤器,需要过滤的对象,通过调用RecognitionFile方法,
* 读取配置文件来确定需要过滤的文件类型。
*/
class FileFilterAvi implements FileFilter {
/*
* accept 是listFiles()方法负责调用,传递参数过来 参数,就是listFiles方法获取到的全路径
* 如果accept方法返回真,获取这个文件 返回假,就放弃这个文件 文件的全路径,判断出文件名是不是.avi的,如果是返回true
*/
public boolean accept(File pathname) {
// return !pathname.getName().endsWith(".avi") &&
// !pathname.isDirectory();
// 创建一个读取配置文件的对象
RecognitionFileType rft = new RecognitionFileType();
// 传入文件类型,并且读取配置文件中,需要过滤文件的后缀
return rft.RecognitionFile(pathname);
/*
* return !pathname.getName().endsWith(".avi") &&
* !pathname.getName().endsWith(".docx") &&
* !pathname.getName().endsWith(".jpg") &&
* !pathname.getName().endsWith(".txt1") &&
* !pathname.getName().endsWith(".doc") &&
* !pathname.getName().endsWith(".rar") &&
* !pathname.getName().endsWith(".zip");
*/
}
}
public class CopyDir {
public static void main(String[] args) {
// 创建一个读取配置文件的对象
RecognitionFileType rft = new RecognitionFileType();
// 读取配置文件中的源目录的路径
String sourceDirectory = rft.ReadConfigurationFile("sourceDirectory")
.replace("\\", "\\\\");
// 读取配置文件中的目的目录的路径
String destinationDirectory = rft.ReadConfigurationFile(
"destinationDirectory").replace("\\", "\\\\");
// System.out.println(sourceDirectory);
// System.out.println(rft.ReadConfigurationFile("destinationDirectory"));
// 将源目录和目的目录路径传入方法中,并且调用复制方法
ergodicDir(new File(sourceDirectory), new File(destinationDirectory));
// ergodicDir(new
// File("C:\\Users\\Administrator\\Desktop\\java学习\\文件接收柜"),new
// File("f:"));
/*
* File file = new File("f:","asd"); if(file.mkdir())
* System.out.println("yes");
*/
// File file = new File("f:\\qwe\\1.txt");
/*
* File file1 = new File("f:\\qwe\\aa\\11.txt");
* file1.getParentFile().getName(); // file.mkdir();
* System.out.println(file1.getParentFile().getName());
*/
}
/*
* 定义方法,实现目录的复制,接收数据源和数据目的
*/
private static void copyDir(File source, File target) {
// 获取数据源目录名字 getName
String dirName = source.getParentFile().getName();
// System.out.println(dirName);
// 将数据目的d: 和目录名组合成File对象,创建同名文件夹
File targetDir = new File(target, dirName);
// System.out.println(targetDir);
// mkdirs创建目录
targetDir.mkdir();
// 遍历数据源,获取数据源目录下的全部文件
// File[] sourceFile = source.getParentFile().listFiles(new
// FileFilterAvi());
// for(File f : sourceFile){
// 获取数据源文件名
String fileName = source.getName();
// 将文件名和目标文件夹,组合成新的File对象
File newFile = new File(targetDir, fileName);
// System.out.println(f);
// System.out.println(newFile);
// IO流读写文件 FileInputStream,FileInputStream 构造器都接收File对象
// System.out.println("...."+newFile);
// 显示已经复制文件的路径
System.out.println("...." + newFile);
// 调用文件复制方法
copyFile(source, newFile);
// }
}
/**
* 功能:复制文件,
*/
private static void copyFile(File soruce, File target) {
// 字节数组缓冲创建
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// 创建流对象
fis = new FileInputStream(soruce);
fos = new FileOutputStream(target);
// System.out.println(soruce);
// System.out.println(target);
// 创建一个字节数组缓冲区
byte[] bytes = new byte[1024];
int len = 0;
// 循环读取字节数组
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
} catch (IOException e) {
throw new RuntimeException("复制失败");
} finally {
try {
if (fos != null)
fos.close();
} catch (IOException e) {
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
}
}
}
}
/**
* 遍历文件夹
*/
private static void ergodicDir(File dir, File target) {
// System.out.println(dir+".....");
// 判断目的目录是不是存在,如果不存在就创建目录
if (!target.exists())
target.mkdir();
// 先获取dir路径下的全部内容
File[] files = dir.listFiles(new FileFilterAvi());
for (File f : files) {
if (f.isDirectory()) {
// 获取数据源目录名字 getName
// String dirName = f.getName();
// System.out.println(dirName);
// 将数据目的d: 和目录名组合成File对象,创建同名文件夹
File targetDir = new File(target, f.getParentFile().getName());
// System.out.println(targetDir);
// mkdirs创建目录
targetDir.mkdirs();
// System.out.println("jinlai "+targetDir);
ergodicDir(f, targetDir);
} else {
// System.out.println(dir+" 111111");
copyDir(f, target);
}
}
}
}
第二部分(注:这部分的代码全部是在另一个.java文件中,文件名:RecognitionFileType.java)
package copyfileall; import java.io.File; import java.io.FileInputStream; import java.util.Properties; public class RecognitionFileType { /* * 功能:读取配置文件,根据传入的参数,并且返回读取的字符串 * */ public static String ReadConfigurationFile(String name) { try { // 流读取配置文件 FileInputStream fis = new FileInputStream("f:\\config.txt"); // 创建集合对象 Properties pro = new Properties(); // load方法 pro.load(fis); // for (int i = 1; i <= pro.size() / 2; i++) { String classN = pro.getProperty(name); return classN; // Class clazz = Class.forName(classN); // Object obj clazz.newInstance(); // Method m = clazz.getMethod(name, parameterTypes) // System.out.println(classN); // } } catch (Exception e) { } return null; /* * !pathname.getName().endsWith(".avi") && * !pathname.getName().endsWith(".docx") && * !pathname.getName().endsWith(".jpg") && * !pathname.getName().endsWith(".txt1") && * !pathname.getName().endsWith(".doc") && * !pathname.getName().endsWith(".rar") && * !pathname.getName().endsWith(".zip"); */ } public static boolean RecognitionFile(File fileName){ //读取配置文件中的需要过滤的文件后缀 String fileSuffix = ReadConfigurationFile("fileSuffix" ); //将读取的字符串,拆分成单独的文件后缀 String[] str = fileSuffix.split(" +"); boolean flag = true; if(str == null) return flag; for(int i = 0 ; i < str.length ; i++){ flag = flag && !fileName.getName().endsWith(str[i]); } return flag; } } |
文件下载:如果需要下载源文件的童鞋,下载地址:http://download.csdn.net/detail/lmh820822/8465413
相关文章推荐
- 通过web.config文件配置项目中文件夹的访问权限
- C#编程连接数据库,通过更改配置文件切换数据库功能。
- SqlServer 更改复制代理配置文件参数及两种冲突策略设置
- 利用TCP 实现通过网络复制文件夹中所有文件!
- c# 通过文件夹共享复制文件到服务器
- 通过外部配置文件更改Javaweb项目的项目名,不通过eclipse编辑
- 通过批处理复制独立存储的文件到桌面并且打开IsolatedStore文件夹
- 3对符合条件的文件和文件夹进行复制或修改
- asp.net项目中通过Web.config配置文件及文件夹的访问权限---forms
- asp.net项目中通过Web.config配置文件及文件夹的访问权限!
- asp.net项目中通过Web.config配置文件及文件夹的访问权限!
- 通过使用FTP复制配置文件
- SqlServer 更改复制代理配置文件参数及两种冲突策略设置
- Apache通过更改配置文件实现url跳转和域名跳转
- asp.net项目中通过Web.config配置文件及文件夹的访问权限!
- CMD命令 (复制、移动、重命名) (文件夹、子文件夹)下所有符合条件的文件
- 通过web.config文件配置项目中文件夹的访问权限
- 通过web.config文件配置项目中文件夹的访问权限
- asp.net项目中通过Web.config配置文件及文件夹的访问权限!
- 通过web.config文件配置项目中文件夹的访问权限