java 把一个文件夹里图片复制到另一个文件夹里
2016-09-20 09:03
525 查看
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Calendar; public class SendServer { private int num = 0; public void process() { 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 void copyFolder(String oldPath, String newPath) { try { File mFile = new File(newPath); if(!mFile .exists()){ (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()) { String fileName = newPath + "/" + (temp.getName()).toString(); File testFile = new File(fileName); if (!testFile.exists()) { FileInputStream input = new FileInputStream(temp); FileOutputStream output = new FileOutputStream(fileName); 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(); num++; } } if (temp.isDirectory()) {// 如果是子文件夹 copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]); } } } catch (Exception e) { System.out.println("复制整个文件夹内容操作出错"); e.printStackTrace(); } } private String getTimeString(String time){ if(time.length()<2){ return "0" + time; } else{ return time; } } }
-----------------------2017/3/22---------------------------------------
获得文件夹下所有文件名
public static class TreeNode { /**节点名称**/ private String text; /**节点路径**/ private String fileName; /**子节点**/ private List<TreeNode> children = new ArrayList<TreeNode>(); } public static TreeNode readfile(TreeNode tn){ try { File file = new File(tn.fileName); tn.text = file.getName(); if (!file.isDirectory()) { //System.out.println(file.getName()); } else if (file.isDirectory()) { String[] filelist = file.list(); for (int i = 0; i < filelist.length; i++) { File readfile = new File(tn.fileName + "\\" + filelist[i]); TreeNode tn1 = new TreeNode(); tn1.text = readfile.getName(); tn1.fileName = tn.fileName + "\\" + filelist[i]; if (!readfile.isDirectory()) { tn.children.add(tn1); } else if (readfile.isDirectory()) { tn.children.add(readfile(tn1)); } } } } catch (Exception e) { System.out.println("readfile() Exception:" + e.getMessage()); } return tn; } public static void main(String[] args){ TreeNode tn = new TreeNode(); tn.fileName = "C:\\Users\\42955\\Desktop\\文件夹\\软件"; readfile(tn); System.out.println(tn); System.out.println(tn.text); }
相关文章推荐
- Java:使用I/O流操作将一个图片复制到另一个文件夹下后打不开的解决方法
- Java IO 复制一个文件到另一个文件夹下
- Java将整个文件夹里的文本中的字符串替换成另外一个字符串(可用于项目复制,变成另一个项目)
- PYTHON实现将一个文件夹下的名字满足条件的图片拷贝复制到另一个文件夹路径
- 复制一个文件夹到另一个文件夹
- [PHP][Function]实现将一个文件夹下的所有文件及文件夹复制到另一个文件夹里(保持原有结构
- Java-一个数组中的元素复制到另一个数组
- 使用C++ 复制一个文件夹下的所有文件到另一个文件夹
- 黑马程序员--把一个多层目录结构的文件夹复制到另一个地方
- [PHP][Function]实现将一个文件夹下的所有文件及文件夹复制到另一个文件夹里(保持原有结构)[已验证][原创]
- 复制一个文件夹下所有文件(包括子文件夹)并修改文件后缀名 java实现加密文档unlock
- 用Java 实现一个表中的数据复制到另一个表中
- Java 复制文件夹所有内容到另一个文件夹
- 复制一个文件夹到另一个文件夹
- 用java的一个复制图片程序
- 将一个文件夹复制到另一个文件夹中
- java ---讲一个文件复制到另一个文件
- java 复制文件到另一个文件夹
- java实现将一个文件夹的所有文件拷贝到另一个文件夹
- IO部分,复制图片到另外一个文件夹中。