一个Java版的目录拷贝程序
2006-07-06 16:26
369 查看
一个Java版的目录拷贝程序
/**
E:/j2sdk1.4.2_09/bin/javac E:/java_s/DirCopyUtil.java
set classpath=E:/java_s
java DirCopyUtil
*/
import java.io.*;
import java.lang.*;
public class DirCopyUtil {
public static void main( String[] args){
if(args.length < 2)
{
System.out.println("usage: DirCopyUtil srcFile dstFile extention");
return;
}
java.io.File src = new java.io.File(args[0]);
java.io.File dst = new java.io.File(args[1]);
try{ DirCopyUtil.copy(src,dst);}
catch(Throwable ex){ex.printStackTrace();}
}
public static void copy(File src, File dest) throws IOException {
getCopyable(src).copy(dest);
}
//this is the factory pattern.
public static Copyable getCopyable(File src) {
//if the source if file then return a file copier
if (src.isFile()) {
return new FileCopier(src);
}
/*else it has to be a directory copier, we could also enhance it to be other type of copier such as httpcopier etc...*/
return new DirCopier(src);
}
interface Copyable {
void copy(File dest) throws IOException;
}
static class DirCopier implements Copyable {
File srcDir;
static ExtensionFileFilter filt = new ExtensionFileFilter();
public DirCopier(File srcDir) {
if (!srcDir.isDirectory()) {
throw new IllegalArgumentException("passed in paremter has to be directory" + srcDir);
}
this.srcDir = srcDir;
}
/**
* copy current directory content under 'destRoot' directory
* @param destRoot the destination root directory, all the file and sub directory will be copied under this
* destRoot directory, if destRoot doesn't exist, this will create the directory.
* @throws IOException
*/
public void copy(File destRoot) throws IOException {
// 1) if destRoot is not exist it will create it
if (!destRoot.exists()) {
if (!destRoot.mkdirs()) {
throw new IOException("unable to create dir:" + destRoot);
}
}
if (!destRoot.isDirectory()) {
throw new IllegalArgumentException("passed in paremter has to be directory" + destRoot);
}
File[] dirContents = srcDir.listFiles(filt);
//we know dirContents can not be null since only file will return null, so no need to check null
for (int i = 0; i < dirContents.length; i++) {
File _srcFile = dirContents[i];
String _srcPath = _srcFile.getCanonicalPath();
/*now I need to get the relative path
question here is c:/abc/ cannonicalpath return "c:/abc" or "c://abc//"? the answer is "c://abc"
so the following statement never executed, but I am just playing safe here since I don't know if
every JVM implementation will behave the same as my XP machine and Sun 1.4 */
if (_srcPath.endsWith(File.separator)) { //the endswith has subtle bug, but it should not matter
_srcPath = _srcPath.substring(0, _srcPath.length() - File.separator.length());
}
File dest = new File(destRoot, _srcPath.substring(_srcPath.lastIndexOf(File.separatorChar) + 1));
//here I wire back to the factory method.
getCopyable(dirContents[i]).copy(dest);
}
}
}
static class FileCopier implements Copyable {
File src;
public FileCopier(File src) {
if (!src.isFile()) {
throw new IllegalArgumentException("passed in paremter has to be file" + src);
}
this.src = src;
}
/**
* Copy the current file to the dest file, if the dest file doesn't exist it will be created.
* @param dest the destination file
* @throws IOException
*/
public void copy(File dest) throws IOException {
if (!dest.exists()) {
dest.createNewFile();
}
if (!dest.isFile()) {
throw new IllegalArgumentException("passed in paremter has to be file" + dest);
}
//do straight file copy
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024 * 4]; //4k buffer
int len = 0;
while ((len = in.read(buffer, 0, buffer.length)) > 0) {
out.write(buffer, 0, len);
}
in.close();
out.close();
}
}
public static class ExtensionFileFilter implements FileFilter {
String extension;
public ExtensionFileFilter(String extension) {
this.extension = extension;
}
public ExtensionFileFilter() {
System.out.println("please input extention:");
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
extension = br.readLine();
}catch(Throwable ex)
{
ex.printStackTrace();
}
}
public boolean accept(File file) {
if(extension == "*.*")
return true;
if(file.isDirectory()){
return true;
}
String name = file.getName();
// find the last
int index = name.lastIndexOf(".");
if(index == -1) {
return false;
} else {
return this.extension.equals(name.substring(index+1));
}
}
}
}
/**
E:/j2sdk1.4.2_09/bin/javac E:/java_s/DirCopyUtil.java
set classpath=E:/java_s
java DirCopyUtil
*/
import java.io.*;
import java.lang.*;
public class DirCopyUtil {
public static void main( String[] args){
if(args.length < 2)
{
System.out.println("usage: DirCopyUtil srcFile dstFile extention");
return;
}
java.io.File src = new java.io.File(args[0]);
java.io.File dst = new java.io.File(args[1]);
try{ DirCopyUtil.copy(src,dst);}
catch(Throwable ex){ex.printStackTrace();}
}
public static void copy(File src, File dest) throws IOException {
getCopyable(src).copy(dest);
}
//this is the factory pattern.
public static Copyable getCopyable(File src) {
//if the source if file then return a file copier
if (src.isFile()) {
return new FileCopier(src);
}
/*else it has to be a directory copier, we could also enhance it to be other type of copier such as httpcopier etc...*/
return new DirCopier(src);
}
interface Copyable {
void copy(File dest) throws IOException;
}
static class DirCopier implements Copyable {
File srcDir;
static ExtensionFileFilter filt = new ExtensionFileFilter();
public DirCopier(File srcDir) {
if (!srcDir.isDirectory()) {
throw new IllegalArgumentException("passed in paremter has to be directory" + srcDir);
}
this.srcDir = srcDir;
}
/**
* copy current directory content under 'destRoot' directory
* @param destRoot the destination root directory, all the file and sub directory will be copied under this
* destRoot directory, if destRoot doesn't exist, this will create the directory.
* @throws IOException
*/
public void copy(File destRoot) throws IOException {
// 1) if destRoot is not exist it will create it
if (!destRoot.exists()) {
if (!destRoot.mkdirs()) {
throw new IOException("unable to create dir:" + destRoot);
}
}
if (!destRoot.isDirectory()) {
throw new IllegalArgumentException("passed in paremter has to be directory" + destRoot);
}
File[] dirContents = srcDir.listFiles(filt);
//we know dirContents can not be null since only file will return null, so no need to check null
for (int i = 0; i < dirContents.length; i++) {
File _srcFile = dirContents[i];
String _srcPath = _srcFile.getCanonicalPath();
/*now I need to get the relative path
question here is c:/abc/ cannonicalpath return "c:/abc" or "c://abc//"? the answer is "c://abc"
so the following statement never executed, but I am just playing safe here since I don't know if
every JVM implementation will behave the same as my XP machine and Sun 1.4 */
if (_srcPath.endsWith(File.separator)) { //the endswith has subtle bug, but it should not matter
_srcPath = _srcPath.substring(0, _srcPath.length() - File.separator.length());
}
File dest = new File(destRoot, _srcPath.substring(_srcPath.lastIndexOf(File.separatorChar) + 1));
//here I wire back to the factory method.
getCopyable(dirContents[i]).copy(dest);
}
}
}
static class FileCopier implements Copyable {
File src;
public FileCopier(File src) {
if (!src.isFile()) {
throw new IllegalArgumentException("passed in paremter has to be file" + src);
}
this.src = src;
}
/**
* Copy the current file to the dest file, if the dest file doesn't exist it will be created.
* @param dest the destination file
* @throws IOException
*/
public void copy(File dest) throws IOException {
if (!dest.exists()) {
dest.createNewFile();
}
if (!dest.isFile()) {
throw new IllegalArgumentException("passed in paremter has to be file" + dest);
}
//do straight file copy
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024 * 4]; //4k buffer
int len = 0;
while ((len = in.read(buffer, 0, buffer.length)) > 0) {
out.write(buffer, 0, len);
}
in.close();
out.close();
}
}
public static class ExtensionFileFilter implements FileFilter {
String extension;
public ExtensionFileFilter(String extension) {
this.extension = extension;
}
public ExtensionFileFilter() {
System.out.println("please input extention:");
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
extension = br.readLine();
}catch(Throwable ex)
{
ex.printStackTrace();
}
}
public boolean accept(File file) {
if(extension == "*.*")
return true;
if(file.isDirectory()){
return true;
}
String name = file.getName();
// find the last
int index = name.lastIndexOf(".");
if(index == -1) {
return false;
} else {
return this.extension.equals(name.substring(index+1));
}
}
}
}
相关文章推荐
- JAVA 程序中如何拷贝一个目录下的文件及子目录到另一个目录,如何获取系统环境变量等...
- 编写一个程序,把指定目录下的所有的带.java文件都拷贝到另一个目录中,拷贝成功后,把后缀名是.java的改成.txt。
- 《Java examples in a nutshell》书中一个关于列出某一目录下的所有目录与文件的程序源代码
- 黑马程序员 编写一个程序,将d:\java目录下的所有.java文件复制到d:\jad目录下,并将原来文件的扩展名从.java改为.jad。
- 面试题 编写一个程序,将e:\project\java目录下的所有.java文件复制到 e:\project\jad目录下,并将原来文件的扩展名从.java改为.jad。
- 编写一个程序,将 d:\java 目录下的所有.java 文件复制到 d:\jad 目录下,并将原来文件的扩展名从.java 改为.jad。
- javaIO_将一个目录下的文件拷贝到另一个目录下(边读边写操作)
- 编写程序,将指定目录下所有.java 文件拷贝到另一个目的中,并将扩展名改为.txt。
- java拷贝目录及其子目录、文件,到另外一个目录
- 用java写了一个实用的类----递归的拷贝给定目录的.java文件到指定的一个文件中
- JAVA web 程序设计的目录结构解读和新建一个程序、部署和运行
- 编写一个程序,将d:\java目录下的所有.java文件复制到d:\jad目录下,并将原来文件的扩展名从.java改为.jad
- 用java程序把本地的一个文件拷贝到hdfs上并显示进度
- [置顶] 使用java来把一个目录下的所有文件拷贝到另外一个目录下,并且重命名
- 编写一个程序,将e:\java目录下的所有.java文件复制到e:\jad目录下
- 黑马程序员—编写程序,将指定目录下所有.java文件拷贝到另一个目的中,并将扩展名改为.txt
- 使用java来把一个目录下的所有文件拷贝到另外一个目录下,并且重命名
- 编写一个程序,将d:\java目录下的所有.java文件复制到d:\jad目录下,并将原来文件的扩展名从.java改为.jad
- 5、 java 编写程序拷贝一个文件, 尽量使用效率高的方式.
- 编写一个程序,将djava目录下的所有.java文件复制到djad目录下,并将原来文件的扩展名从.java改为.jad