java 把一个文件夹里图片复制到另一个文件夹里
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Calendar; public class SendServer { private int num = ; 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 = ;
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 = ; 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[ * ];
int len;
while ((len = input.read(b)) != -) {
output.write(b, , 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()<){
return "" + 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 把一个文件夹里图片复制到另一个文件夹里的更多相关文章
- SQL数据库中把一个表中的数据复制到另一个表中
1.如果是整个表复制表达如下: insert into table1 select * from table2 2.如果是有选择性的复制数据表达如下: insert into table1(colu ...
- 把一个List<T>的数据复制至另一个List<T>
把一个数据集List<T>复制至到另一个数据集List<T>. 方法一,可以使用循环,然后把每一个T添加至另一个集合中去: public void ListDemo() { , ...
- Java将一个目录下的所有数据复制到另一个目录下
/* 将"C:\\JavaProducts\\Source"下的所有数据复制到"C:\\Target"下 */ import java.io.*; public ...
- VMWare12虚拟机实现主客机间的文件拖拽(复制粘贴)和文件夹共享
版本: 主机:Windows 7 64位旗舰版 虚拟机: VMWare 12 + Windows 7 64位旗舰版 VMWare pro 12 + Ubuntu16.04LTS 64位 注:由于VMW ...
- 用Java 实现一个表中的数据复制到另一个表中
string sql = "insert into tbl1(s1,s2,s3) select t.t1,t.t2,t.t3 from tab2 t";再用jdbc或者hibern ...
- oracle怎么把一个用户下的表复制给另一个用户?(授予表权限)
//把system读写权限 授权给scottselect 'Grant all on '||table_name||' to scott;' from all_tables where owner = ...
- Java以流的方式将指定文件夹里的.txt文件全部复制到另一文件夹,并删除原文件夹中所有.txt文件
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- Git学习:如何在Github的README.MD文件下添加图片
格式如下:  关于图片的绝对路径: 必须把图片上传到github的代码仓库里,再将其图片的网址复制到括号里才可以,不能够直接把图片复制到readme.md文件里面,这 ...
- 巧用 git rebase 将某一部分 commit 复制到另一个分支
一.为什么需要将一个 commit 复制到其他分支上去呢? 在我们的实际开发的过程中,我们的项目中会存在多个分支. 在某些情况下,可能需要将某一个分支上的 commit 复制到另一个分支上去. 二 ...
随机推荐
- Git合并特定commits 到另一个分支
https://ariejan.net/2010/06/10/cherry-picking-specific-commits-from-another-branch/ http://blog.csdn ...
- [moka同学笔记]Yii2.0给一张表中增加一个属性
1.model中建立关联 public function getUser(){ return$this->hasOne(User::className(),['id'=>'uid']) ; ...
- Java的整个字符串的结束索引在最后一个字符之外
/** * Created by xfyou on 2016/11/4. */ public class SubstringDemo { public static void main(String[ ...
- emberjs学习二(ember-data和localstorage_adapter)
emberjs学习二(ember-data和localstorage_adapter) 准备工作 首先我们加入ember-data和ember-localstorage-adapter两个依赖项,使用 ...
- 初识mfc
今天主要了解了Visual C++的开发环境Visual Studio(话说以前都是用来调试控制台程序的)和用mfc写了一个最简单的程序. 目前微软大力推广的开发环境就是vs,它的集成度相当高,方便程 ...
- 在 Xamarin.Android 中使用 Notification.Builder 构建通知
0 背景 在 Android 4.0 以后,系统支持一种更先进的 Notification.Builder 类来发送通知.但 Xamarin 文档含糊其辞,多方搜索无果,遂决定自己摸索. 之前的代码: ...
- SharePoint 2013 为站点配置基于主机标头的双域名
SharePoint的应用中,经常需要配置双域名,为不同的认证方式提供访问入口,下面简单介绍下,如何以主机标头的方式为SharePoint配置双域名: 配置基于主机标头的双域名 1.原本可以访问的测试 ...
- GPS定位为什么要转换处理?高德地图和百度地图坐标处理有什么不一样?
GPS定位为什么要转换处理?高德地图和百度地图坐标处理有什么不一样? 先了解一下 高德地图 采用: GCJ-02 (不可逆) 百度百科: http://baike.baidu.com/link?url ...
- JavaScript学习05 定时器
JavaScript学习05 定时器 定时器1 用以指定在一段特定的时间后执行某段程序. setTimeout(): 格式:[定时器对象名=] setTimeout(“<表达式>”,毫秒) ...
- CoreDataStack