⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fcsupper.java

📁 一个用java开发的文件拷贝程序
💻 JAVA
字号:
/*
*  FCSuper.java
*  平台: window 2000
*			 JDK 1.4.0
*  作者:张锋
*  日期:2003/08/26
*  描述:
*		将指定的文件copy 到指定的目录下
*/
import java.util.*;
import java.io.*;
import java.text.*;

public class FCSupper {
	
	
	private String to_dir; // 目标目录路径
	
	private Vector vFileList = null; // 要拷贝的文件列表
	private Vector vCreateDir = null; //要纵度创建文件的文件加列表
	private Vector vIgnoreDir = null; //搜索时需要忽略的目录
		
	private String dirflg = "";  //文件拷贝方式 1:对应目录拷贝 2:新目录创建拷贝
	private String old_dir ="";  //为对应目录拷贝时,原文件的父目录
	private String logfile =""; //日志文件
	
	/*
	* 构造函数
	*/
	public FCSupper(){
		vFileList  = new Vector();
		vCreateDir = new Vector();
		vIgnoreDir = new Vector();
	}
	/*
	* 屏幕显示提示方法,和客户的接口
	*/
	public void mainDisply(){
		String from_dir= ""; //元文件目录路径
		System.out.println("Welcome Use FileCopySupper!!");
		System.out.println("If you want to exit ,enter  Q and q !!");
		BufferedReader in = null;
		int flg = -1;
		//文件的拷贝方式
		System.out.println("Copy Mode: (1)、Corresponding Dircetory Copy."); 
		System.out.println("           (2)、New Directory Copy.(select 1 or 2)");
		System.out.flush();
		try{
			while( flg < 0){			
				in = new BufferedReader(new InputStreamReader(System.in));
				String response = in.readLine();
				if(response != null && (response.equals("1") || response.equals("2"))){
					dirflg = response;
					flg = 1;
				}else{
					 if(response != null && (response.equals("Q") || response.equals("q"))){
						System.out.println("Exit the system? (Y/N)");
						System.out.flush();
						in = new BufferedReader(new InputStreamReader(System.in));
						String ret = in.readLine();
						if(ret !=null && (ret.equals("Y") || ret.equals("y"))){
							System.exit(0);
						}else{
							System.out.println("input error! enter agin:");
						}
					}else{
						System.out.println("input error! enter agin:");
					}				
				}
			} 
			//原目录路径			
			flg = -1;
			System.out.println("Enter the source Directory:");
			System.out.flush();
			while(flg < 0){			
				in = new BufferedReader(new InputStreamReader(System.in));
				from_dir = in.readLine();
				File sour_dir = new File(from_dir);
				if(from_dir.equals("Q") || from_dir.equals("q")){
					System.out.println("Exit the system? (Y/N)");
					System.out.flush();
					in = new BufferedReader(new InputStreamReader(System.in));
					String ret = in.readLine();
					if(ret !=null && (ret.equals("Y") || ret.equals("y"))){
						System.exit(0);
					}else{
						System.out.println("The directory is not exists! enter agin:");	
					}
				}else if( !sour_dir.exists()){
					System.out.println("The directory is not exists! enter agin:");				
				}else if( !sour_dir.isDirectory()){
					System.out.println("Is not a directory! enter agin:");				 
				}else{
					flg = 1;
				}
			}
			//目标目录路径
			flg = -1;
			System.out.println("Enter the aim Directory:");
			System.out.flush();
			while(flg <0){
				in = new BufferedReader(new InputStreamReader(System.in));
				to_dir = in.readLine();
				File aim_dir = new File(to_dir);
				if(to_dir.equals("Q") || to_dir.equals("q")){
					System.out.println("Exit the system? (Y/N)");
					System.out.flush();
					in = new BufferedReader(new InputStreamReader(System.in));
					String ret = in.readLine();
					if(ret !=null && (ret.equals("Y") || ret.equals("y"))){
						System.exit(0);
					}else{
						System.out.println("Enter the aim Directory agin:");
					}
				}else if( !aim_dir.exists()){
					System.out.println("This directory is not exists! create new directory?(Y/N)");
					in = new BufferedReader(new InputStreamReader(System.in));
					String create = in.readLine();
					if(create.equals("y") ||create.equals("Y")){
						boolean retflg = aim_dir.mkdirs();
						if( !retflg){
							System.out.println("Input Error ! enter agin:");
						}else{
							flg = 1;
						}
					}else{
						System.exit(0);
					}
				}else if( !aim_dir.isDirectory()){
					System.out.println("Is not a directory! enter agin:");
				}else{
					flg = 1;
				}
			}
		}catch(Exception e){}		 
		System.out.println("File Copy Start! Please waite......");
		getFileList("proptery\\fileList.txt","0");
		getFileList("proptery\\notFindDir.txt","1");
		if(dirflg.equals("2")){
			getFileList("proptery\\createDir.txt","2");
		}else{
			getFileList("proptery\\source.txt","3");
		}
		logfile =""+System.currentTimeMillis() + ".txt"; 
		getAllFile(from_dir);
		System.out.println("File Copy Complete! Please look at the logfile:" + logfile);
	}
	/*
	* 读取文件设置 
	*/
	public void getFileList(String fileList, String flg ){
		BufferedReader in = null;
		try{
			in = new BufferedReader(new FileReader(fileList));
			String line = "";		
			while( (line = in.readLine()) != null){				
				if(flg.equals("0")){
					vFileList.add(line);
				}
				if(flg.equals("1")){
					vIgnoreDir.add(line);
				}
				if(flg.equals("2")){
					vCreateDir.add(line);
				}
				if(flg.equals("3")){
					old_dir = line;
					break;
				}
			}
		}catch(Exception e){
			System.out.println("get file List error :" + e);
		}finally{
			if( in != null) try{ in.close();}catch(Exception e){}			
		}
	}
	// 递归读取元目录路径下的文件,如果满足要求,拷贝到目标目录路径
	public void getAllFile(String fileDir){
		File dir = new File(fileDir);
		File[] list = dir.listFiles();		
		int total =0;
		for ( int i=0; list != null && i < list.length ; i++ ){									   
				if(list[i].isDirectory()){					
					String path = list[i].getPath();
					String fileName = list[i].getName();
					if(vIgnoreDir.indexOf(fileName) != -1){
						total++;
					}else{
						getAllFile(path);
					}
				}else{					
					fileCopy(list[i]);
					total++;
				}
		}
		if(total == list.length){
			return ;
		}
	}
	
	//文件拷贝准备,根据属性设置文件的要求,组织程序目录
	public void fileCopy(File from){
		String path = from.getParent();
		String fileName = from.getName();		
		String newFile = "";
		if( vFileList.indexOf(fileName) == -1){
			return;
		}
		if(dirflg.equals("1")){	
			int index = path.indexOf(old_dir);		
			if( index != -1){
				path = to_dir + path.substring(old_dir.length());
			}					
			
		}else{
			for(int i =0;vCreateDir != null &&  i < vCreateDir.size(); i++){
				String creatdir = (String)vCreateDir.get(i);
				int index = path.indexOf(creatdir);
				if( index != -1){
					path =  to_dir + "\\" + path.substring(index + creatdir.length() + 1 );
					break;
				}
			}
		}
		File dir = new File(path);
		if( !dir.exists()){
			dir.mkdirs();          
		}
		path = path + "\\" +fileName;
		fileRealCopy(from, path);		
	}
	//文件的拷贝
	public void fileRealCopy(File from, String to_path){
		
		String now_date = new SimpleDateFormat("yyyyMMddHH:mm:ss.SSS").format(new Date(System.currentTimeMillis()));
		System.out.println(to_path);
		FileInputStream from_file = null;
		FileOutputStream to_file = null;
		PrintWriter out = null;		
		try{			
			from_file = new FileInputStream(from);
			File tempTopath = new File(to_path);	
			if( !tempTopath.canWrite()){
				tempTopath.delete();
			}					
			to_file = new FileOutputStream(tempTopath);			
			File outTemp = new File("log\\" +logfile);
			if( outTemp.exists())
				out = new PrintWriter( new OutputStreamWriter( new FileOutputStream(outTemp,true)));
			else
				out = new PrintWriter( new OutputStreamWriter( new FileOutputStream(outTemp)));	
			byte[] buffer = new byte[4096];
			int bytes_read;				
			while((bytes_read = from_file.read(buffer)) != -1){
				to_file.write(buffer, 0, bytes_read);
			}				
			out.println( "[" +now_date +"] " +" File: " + from.getName() + "  Copy right!");
		}catch(Exception e){			
			System.out.println("File Copy error!" + e);	
			out.println( "File: " + from.getName() + "  Copy Error:"+ e);		
		}
		finally{
			if( from_file != null) try{ from_file.close();}catch(Exception e){}
			if( to_file != null) try{ to_file.close();}catch(Exception e){}
			if( out != null) try{ out.close();}catch(Exception e){}
		}
	}
	public static void  main(String[] args){
		FCSupper fc = new FCSupper();
		fc.mainDisply();
	}
	
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -