如题,程序将一个文件夹中jpg格式文件拷贝到另一个文件夹中。程序使用了多线程和递归。多线程的方法明显慢很多。请高手指点一下。。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class FileCelectThread {
static int name = 1;
static String path1 = "d:\\ka1";
public static void main(String[] args) throws FileNotFoundException {
String path = "E:\\Documents and Settings\\lio\\桌面\\卡系列";
File f = new File(path);
//多线程阶段
ExecutorService executorService = Executors.newCachedThreadPool();
ScanFile sc = new ScanFile(executorService,f);
executorService.submit(sc);
//递归代码
File f1 = new File(path1);
if(!f1.exists()){
f1.mkdir();
}
move(f);
}
static void move(File opFile){
if(opFile.exists()){
if(opFile.isDirectory()){
File[] f = opFile.listFiles();
if(f!=null&&f.length>0){
for(File tmp:f){
if(tmp.isDirectory()){
move(tmp);
}else if(tmp.getName().endsWith(".jpg")){
try{
FileInputStream inputStream = new FileInputStream(tmp);
FileOutputStream output = new FileOutputStream(path1+"\\"+name+".jpg");
byte[] buffer = new byte[1024];
int n = 0;
try{
while (-1 != (n = inputStream.read(buffer))) {
output.write(buffer, 0, n);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
output.close();
inputStream.close();
name++;
}catch(Exception e1){
e1.printStackTrace();
}
}
}catch(FileNotFoundException fe){
}
}
}
}
}
}
}
}class ScanFile implements Runnable{
static Integer name = 1;
static String path = "d:\\ka";
ExecutorService executorService;
File opFile;
public ScanFile(ExecutorService executorService,File f) {
this.executorService = executorService;
opFile = f;
File d = new File(path);
if(!d.exists()){
d.mkdir();
}
} public void run() {
if(opFile.exists()){
if(opFile.isDirectory()){
File[] f = opFile.listFiles();
if(f!=null&&f.length>0){
for(File tmp:f){
if(tmp.isDirectory()){
ScanFile scan = new ScanFile(executorService, tmp);
executorService.submit(scan);
}else if(tmp.getName().endsWith(".jpg")){
try{
FileInputStream inputStream = new FileInputStream(tmp);
FileOutputStream output = new FileOutputStream(path+"\\"+name+".jpg");;
byte[] buffer = new byte[1024];
int n = 0;
try{
while (-1 != (n = inputStream.read(buffer))) {
output.write(buffer, 0, n);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
output.close();
inputStream.close();
}catch(Exception e1){
e1.printStackTrace();
}
}
}catch(FileNotFoundException fe){
}
}
}
}
}
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class FileCelectThread {
static int name = 1;
static String path1 = "d:\\ka1";
public static void main(String[] args) throws FileNotFoundException {
String path = "E:\\Documents and Settings\\lio\\桌面\\卡系列";
File f = new File(path);
//多线程阶段
ExecutorService executorService = Executors.newCachedThreadPool();
ScanFile sc = new ScanFile(executorService,f);
executorService.submit(sc);
//递归代码
File f1 = new File(path1);
if(!f1.exists()){
f1.mkdir();
}
move(f);
}
static void move(File opFile){
if(opFile.exists()){
if(opFile.isDirectory()){
File[] f = opFile.listFiles();
if(f!=null&&f.length>0){
for(File tmp:f){
if(tmp.isDirectory()){
move(tmp);
}else if(tmp.getName().endsWith(".jpg")){
try{
FileInputStream inputStream = new FileInputStream(tmp);
FileOutputStream output = new FileOutputStream(path1+"\\"+name+".jpg");
byte[] buffer = new byte[1024];
int n = 0;
try{
while (-1 != (n = inputStream.read(buffer))) {
output.write(buffer, 0, n);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
output.close();
inputStream.close();
name++;
}catch(Exception e1){
e1.printStackTrace();
}
}
}catch(FileNotFoundException fe){
}
}
}
}
}
}
}
}class ScanFile implements Runnable{
static Integer name = 1;
static String path = "d:\\ka";
ExecutorService executorService;
File opFile;
public ScanFile(ExecutorService executorService,File f) {
this.executorService = executorService;
opFile = f;
File d = new File(path);
if(!d.exists()){
d.mkdir();
}
} public void run() {
if(opFile.exists()){
if(opFile.isDirectory()){
File[] f = opFile.listFiles();
if(f!=null&&f.length>0){
for(File tmp:f){
if(tmp.isDirectory()){
ScanFile scan = new ScanFile(executorService, tmp);
executorService.submit(scan);
}else if(tmp.getName().endsWith(".jpg")){
try{
FileInputStream inputStream = new FileInputStream(tmp);
FileOutputStream output = new FileOutputStream(path+"\\"+name+".jpg");;
byte[] buffer = new byte[1024];
int n = 0;
try{
while (-1 != (n = inputStream.read(buffer))) {
output.write(buffer, 0, n);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
output.close();
inputStream.close();
}catch(Exception e1){
e1.printStackTrace();
}
}
}catch(FileNotFoundException fe){
}
}
}
}
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货