package com.gz.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Scanner;
import java.util.Vector;
/*
* 1,指定源文件夹路径
* 2,指定要合并的文件类型,
* 3,遍历出源文件夹目录下所有该类型的文件,并显示总大小
* 4,合并该类型的所有文件到要输出的文件夹目录中,如果没有则创建一个
* */
public class CombineFile {
public static long count = 0, length = 0;
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
System.out.println("本程序功能是合并文件");
File dir = getFile();
Scanner sc = new Scanner(System.in);
System.out.println("您真的要合并" + dir.getName() + "里的文件吗?请选择y/n");
while (true) {
String line = sc.nextLine();
if ("y".equals(line)) {
break;
} else if ("n".equals(line)) {
return;
} else
System.out.println("小伙子,你输错了,请看清楚再输入");
}
System.out.println(dir.getName() + "路径下的所有文件为:");
printFile(dir);
System.out.println(dir.getName() + "目录下共有" + count + "个文件, 共" + length
+ "b");
System.out.println("请指定要合并的文件类型");
String line = sc.nextLine();
printXxxFile(dir, line);
System.out.println(dir.getName() + "目录下共有" + count + "个" + line + "文件");
System.out.println("您真的要合并" + dir.getName() + "里的文件吗?请选择y/n");
while (true) {
String s = sc.nextLine();
if ("y".equals(s)) {
break;
} else if ("n".equals(s)) {
return;
} else
System.out.println("小伙子,你输错了,请看清楚再输入");
}
System.out.println("请指定输出目录");
String str = sc.nextLine();
File des = new File(str);
long start=System.currentTimeMillis();
combineFile(dir, line, des);
long end =System.currentTimeMillis();
System.out.println("合并已完成!耗时"+(end-start)+"ms");
}
public static void combineFile(File dir, String line, File des)
throws IOException {
if (!des.exists()) {
des.mkdir();
}
File[] arr = dir.listFiles();
if (arr == null) {
return;
}
Vector<FileInputStream> v = new Vector<>();
for (File file : arr) {
FileInputStream fis = new FileInputStream(file);
v.add(fis);
}
Enumeration<FileInputStream> en = v.elements();
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream(
new File(des, "终极合并" + line));
byte[] arry = new byte[1024 * 8];
int len;
while ((len = sis.read(arry)) != -1) {
fos.write(arry, 0, len);
}
sis.close();
fos.close();
}
public static void printXxxFile(File dir, String line) {
File[] subFiles = dir.listFiles();
if (subFiles == null) {
return;
}
for (File file : subFiles) {
if (file.isFile() && file.getName().endsWith(line)) {
System.out.println(file + ":");
System.out.println("num=" + file.length() + "b");
count++;
} else if (file.isDirectory()) {
printXxxFile(file, line);
}
}
}
public static long printFile(File dir) {
File[] subFiles = dir.listFiles();
if (subFiles == null) {
return 0;
}
for (File file : subFiles) {
if (file.isFile()) {
System.out.println(file.getName() + ":");
System.out.println("num=" + file.length() + "b");
count++;
length += file.length();
} else if (file.isDirectory()) {
length += printFile(file);
}
}
return length;
}
public static File getFile() {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
System.out.println("请输入文件夹路径:");
while (true) {
String line = sc.nextLine();
File dir = new File(line);
if (!dir.exists()) {
System.out.println("您输入的路径不存在,请重新输入");
} else if (dir.isFile()) {
System.out.println("您输入的是文件路径,请重新输入文件夹路径");
} else {
return dir;
}
}
}
}
|
|