`
fenglingxuewqk
  • 浏览: 82055 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

java复制和移动文件

阅读更多

复制文件

 

private void copyFile(File in, File out) {
	try {
		FileChannel sourceChannel = new FileInputStream(in).getChannel();
		FileChannel destinationChannel = new FileOutputStream(out)
			.getChannel();
		sourceChannel.transferTo(0, sourceChannel.size(),
			destinationChannel);
		// or
		// destinationChannel.transferFrom(sourceChannel, 0,
		// sourceChannel.size());
		sourceChannel.close();
		destinationChannel.close();
		sourceChannel = null;
		destinationChannel = null;
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

 

移动文件:

private void movefile(String srcFile, String destPath) {
	File oldfile = new File(srcFile);
	File newpath = new File(destPath);
	if (!newpath.exists()) {
		newpath.mkdirs();
	}

	File newfile = new File(destPath, oldfile.getName());
	oldfile.renameTo(newfile);
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics