java将图片写入一个文件夹_java 把一个文件夹里图片复制到另一个文件夹里

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.Calendar;public classSendServer {private int num = 0;public voidprocess() {

Calendar calendar=Calendar.getInstance();

String dir= calendar.get(Calendar.YEAR) + “” + getTimeString(calendar.get(Calendar.MONTH) + “”);

String oldPath= “/img2” +dir;

String newPath= “/img5” +dir;try{

while(true){

System.out.println(“复制” + oldPath + “目录开始”);long t1 =System.currentTimeMillis();

num= 0;

copyFolder(oldPath, newPath);long t2 =System.currentTimeMillis();

System.out.println(“复制目录结束,用时:” + (t2-t1) + “ms,共复制:” + num + “文件”);

}

}catch(Exception ex) {

ex.printStackTrace();

}

}public voidcopyFolder(String oldPath, String newPath) {try{

File mFile= newFile(newPath);if(!mFile .exists()){

(new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹

}

File a= newFile(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()) {

String fileName= newPath + “/” +(temp.getName()).toString();

File testFile= newFile(fileName);if (!testFile.exists()) {

FileInputStream input= newFileInputStream(temp);

FileOutputStream output= newFileOutputStream(fileName);byte[] b = new byte[1024 * 5];intlen;while ((len = input.read(b)) != -1) {

output.write(b,0, len);

}

output.flush();

output.close();

input.close();

num++;

}

}if (temp.isDirectory()) {//如果是子文件夹

copyFolder(oldPath + “/” + file[i], newPath + “/” +file[i]);

}

}

}catch(Exception e) {

System.out.println(“复制整个文件夹内容操作出错”);

e.printStackTrace();

}

}privateString getTimeString(String time){if(time.length()<2){return “0” +time;

}else{returntime;

}

}

}

    原文作者:饿鱼小孩儿
    原文地址: https://blog.csdn.net/weixin_29357339/article/details/114621996
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞