`
wxinpeng
  • 浏览: 581701 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类
最新评论

java copy 文件夹

阅读更多
/**
 * copy 文件夹
 */


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyFile {
	private void copyFile(File source, File target) {// copy 文件
		FileInputStream inFile = null;
		FileOutputStream outFile = null;
		try {
			inFile = new FileInputStream(source);
			outFile = new FileOutputStream(target);
			byte[] buffer = new byte[1024];
			int i = 0;
			while ((i = inFile.read(buffer)) != -1) {
				outFile.write(buffer, 0, i);
			}
			inFile.close();
			outFile.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (inFile != null) {
					inFile.close();
				}
				if (outFile != null) {
					outFile.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	// 处理目录
	public void copyDict(File source, File target) {
		File[] file = source.listFiles();// 得到源文件下的文件项目
		for (int i = 0; i < file.length; i++) {
			if (file[i].isFile()) {// 判断是文件
				File sourceDemo = new File(source.getAbsolutePath() + "/"
						+ file[i].getName());
				File destDemo = new File(target.getAbsolutePath() + "/"
						+ file[i].getName());
				this.copyFile(sourceDemo, destDemo);
			}
			if (file[i].isDirectory()) {// 判断是文件夹
				File sourceDemo = new File(source.getAbsolutePath() + "/"
						+ file[i].getName());
				File destDemo = new File(target.getAbsolutePath() + "/"
						+ file[i].getName());
				destDemo.mkdir();// 建立文件夹
				this.copyDict(sourceDemo, destDemo);
			}
		}// end copyDict

	}
}

 

分享到:
评论
8 楼 gmingsoft04 2008-12-08  
顺便贴一个别人写的:java删除文件及目录

引用
[b]public static void deleteFile(String filePath){
File f = new File(filePath);
if(f.exists() && f.isDirectory()){
File delFiles[]= f.listFiles();
for(int i = 0;i<delFiles.length;i++){
deleteFile(delFiles[i].getAbsolutePath());
}
}
f.delete();
}[/b]
7 楼 gmingsoft04 2008-12-08  
不好意思:
FileLenthWithKB()这个方法写错了
:)
	public static String FileLenthWithKB(File f)
	{
		long fileSize=f.length();
		if(fileSize<1024)
			return 1+"KB";
		else
			return fileSize/1024+"KB";		
	}


return 1+"KB";不是return 1l+"KB";
6 楼 gmingsoft04 2008-12-08  
文件的大小:file.length()
单位是:字节  (Bytes)
所以如果单位是KB的话,
则可以这样:

public static String FileLenthWithKB(File f)
{
long fileSize=f.length();
if(fileSize<1024)
return 1l+"KB";
else
return fileSize/1024+"KB";
}

public static String FileLenthWithBytes(File f)
{
long fileSize=f.length();
return fileSize+"B";
}
5 楼 gmingsoft04 2008-12-08  
输出如下:
Java-Collections-Framework.pdf的属性如下:
是文件
不是目录
可读取
可写入
不是隐藏文件
文件的最后修改日期为:Sat Dec 06 09:56:41 CST 2008
文件的大小为:359247B
文件后缀为.pdf
文件MIME类型为pdf
4 楼 gmingsoft04 2008-12-08  
晕,这个编辑器还不是很会用啊.
这句没有显示完整.
String targetDirPath = target.getAbsolutePath().substring(0,target.getAbsolutePath().lastIndexOf("\\"));
这句的作用就是截取字符串:
C://311//11111.txt
因为C:/311可能不存在
如果不创建这个目录,将可能会报IOException,
系统找不到指定的目录.
所以在复制文件前,要检查有没有这个目录存在.
没有,则创建一个目录.
我的代码如下:
public static void main(String[] args) throws IOException{
File file= new File("D://211//Java-Collections-Framework.pdf");

System.out.println(file.getName()+"的属性如下:");
System.out.println(file.isFile()?"是文件":"不是文件");
System.out.println(file.isDirectory()?"是目录":"不是目录");
System.out.println(file.canRead()?"可读取":"不可读取");
System.out.println(file.canWrite()?"可写入":"不可写入");
System.out.println(file.isHidden()?"是隐藏文件":"不是隐藏文件");
System.out.println("文件的最后修改日期为:"+new Date(file.lastModified()));
System.out.println("文件的大小为:"+file.length()+"B");
System.out.println("文件后缀为"+file.getName().substring(file.getName().lastIndexOf(".")));
System.out.println("文件MIME类型为"+file.getName().substring(file.getName().lastIndexOf(".")+1));
FileInfo f= new FileInfo(file);
f.printFileInfo();
//System.out.println(f.getFileCreateDate(file));
File src = new File("D://211//211-2.txt");
File target = new File("C://311//11111.txt");
System.out.println(target.getAbsolutePath());
System.out.println(target.getAbsolutePath().lastIndexOf("\\"));
String targetDirPath = target.getAbsolutePath().substring(0,
target.getAbsolutePath().lastIndexOf("\\"));
File fdir = new File(targetDirPath);
if(!fdir.exists())
{
fdir.mkdirs();
}
System.out.println(targetDirPath);
copyFile(src, target);
File srcDir = new File("D://211");
File targetDir = new File("C://110");
if (!targetDir.exists()) {
targetDir.mkdirs();
}
copyDict(srcDir, targetDir);

}
3 楼 gmingsoft04 2008-12-08  

		File src = new File("D://211//211-2.txt");
		File target = new File("C://311//11111.txt");
		System.out.println(target.getAbsolutePath());
		System.out.println(target.getAbsolutePath().lastIndexOf("\\"));
		String targetDirPath = target.getAbsolutePath().substring(0,
				target.getAbsolutePath().lastIndexOf("\\"));
		File fdir = new File(targetDirPath);
		fdir.mkdirs();
		System.out.println(targetDirPath);
		copyFile(src, target);
		File srcDir = new File("D://211");
		File targetDir = new File("C://110");
		if (!targetDir.exists()) {
			targetDir.mkdirs();
		}
		copyDict(srcDir, targetDir);

2 楼 gmingsoft04 2008-12-08  
good File Util.
File src = new File("D://211//211-2.txt");
		File target=new File("C://311//11111.txt");
		System.out.println(target.getAbsolutePath());
		System.out.println(target.getAbsolutePath().lastIndexOf("\\"));
		String targetDirPath=target.getAbsolutePath().substring(0,target.getAbsolutePath().lastIndexOf("\\"));
		File fdir= new File(targetDirPath);
		fdir.mkdirs();
		System.out.println(targetDirPath);
		copyFile(src,target);
		File srcDir = new File("D://211");
		File targetDir = new File("C://110");
		if(!targetDir.exists())
		{
			targetDir.mkdirs();
		}
		copyDict(srcDir,targetDir);
		
	}
1 楼 herolin 2008-08-20  
大大您好,
小弟的文章http://herolin.mine.nu/entry/java-copy-file-directory有参考一下您的文章,特来告知
谢谢

相关推荐

Global site tag (gtag.js) - Google Analytics