JAVA进阶——文件系统

这次针对文件系统的操作做了一个FileTool类,主要是为了学会File类的使用。

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;


public class FileTool {
	
	private static void checkFile( File source, File dest ) throws Exception{
		if( !source.exists() || !source.isFile() ){//源文件不存在,或者源文件是目录
			throw new RuntimeException( "File Copy: no shuch source file: " + source.getAbsolutePath() );
		}
		
		if( !source.canRead() ){//源文件不可读
			throw new RuntimeException( "File Copy: source file can not be read." );
		}
		
		if( dest.exists() ){//目标文件已经存在
			if( dest.isFile() ){//目标是一个文件,把它删除
				dest.delete();
			}else{//目标不是文件,那么就是目录
				throw new RuntimeException( "File Copy: destination: " + dest.getAbsolutePath() +
						" is not a file" );
			}
		}else{//目标文件在复制之前不存在
			
			File parent = new File( dest.getParent() );//目标文件的父目录
			if( !parent.exists() ){//父目录不存在,非法路径
				throw new RuntimeException( "File Copy: destination " + dest.getAbsolutePath() + 
						" directory dosen't exist.");
			}
			
			if( !parent.canWrite() ){//在父目录下,无写操作的权限
				throw new RuntimeException( "File Copy: destination " + dest.getAbsolutePath() +
						" directory can not be written." );
			}
		}
	}
	

	public static void deleteFile( String path ) throws IOException {
		File f = new File( path );
		if( f.exists() && f.isDirectory() ){
			if( f.listFiles().length==0 ){//目录是空的,直接删除
				f.delete();
			}else{//目录是非空的,先把目录中的文件和子目录删除,再删除本目录
				File[] files = f.listFiles();
				for( int i=0;i<files.length;++i ){
					if( files[i].isDirectory() ){//删除子目录
						FileTool.deleteFile( files[i].getAbsolutePath() );
					}
					
					files[i].delete();//删除文件
				}
				f.delete();//删除本目录
			}
		}
	}
	
	
	public static void copyFile( String sourcePath, String destPath) throws IOException {
		File source = new File( sourcePath );
		File dest = new File( destPath );
		
		FileInputStream in = null;
		FileOutputStream out = null;
		
		BufferedInputStream bin = null;
		BufferedOutputStream bout = null;
		
		
		try{
			checkFile( source, dest );
			
			in = new FileInputStream( source );
			out = new FileOutputStream( dest );
			
			bin = new BufferedInputStream( in );
			bout = new BufferedOutputStream( out );
			
			byte[] buffer = new byte[1024];
			int byteRead = 0;
			while( true ){
				byteRead= bin.read( buffer );
				if( byteRead==-1 ){
					break;
				}
				bout.write( buffer,0,byteRead );
			}
			}catch( Exception e ){
				e.printStackTrace();
			}finally{
				if( bin!=null ){
					bin.close();
				}
				if( bout!=null ){
					bout.close();
				}
		}
	}

	public static void copyDirectory( String sourcePath, String destPath ) throws IOException{
		File source = new File( sourcePath );
		if( !source.exists() ){
			throw new RuntimeException( "Copy Direactory: " + sourcePath + " does not exist." );
		}
		
		if( !source.canRead() ){
			throw new RuntimeException( "Copy Directory: " + sourcePath + " can not be read." );
		}
		
		File dest = new File( destPath );
		
		if( !dest.getParentFile().canWrite() ){
			throw new RuntimeException( "Copy Directory: " + destPath + " can not be written." );
		}
		
		if( !dest.exists() ){
			dest.mkdirs();
		}
		
		File[] files = source.listFiles();
		for( int i=0;i<files.length;++i ){
			if( files[i].isFile() ){
				BufferedInputStream in = new BufferedInputStream(
						new FileInputStream( files[i] ) );
				BufferedOutputStream out = new BufferedOutputStream(
						new FileOutputStream( destPath + "/" + files[i].getName() ) );
				byte[] buffer = new byte[1024];
				int byteRead = 0;
				while( true ){
					in.read(buffer);
					if( byteRead==-1 ){
						break;
					}
					out.write(buffer, 0, byteRead);
				}
				
				in.close();
				out.close();
			}
			
			if( files[i].isDirectory() ){
				FileTool.copyDirectory(files[i].getAbsolutePath(),
						destPath + "/" + files[i].getName() );
			}
		}
	}
	
	public static void moveFileOrDirectory( String sourcePath, String destPath ) throws IOException {
		File source = new File( sourcePath );
		File dest = new File( destPath );
		
		try{
			checkFile( source, dest );
			source.renameTo( dest );
		}catch( Exception e ){
			e.printStackTrace();
		}
	}
	
	public static void append( String sourcePath, String destPath ) throws IOException {
		File source = new File( sourcePath );
		File dest = new File( destPath );
		
		BufferedInputStream bin = null;
		RandomAccessFile raf = null;
		
		try{
			checkFile( source, dest );
			bin = new BufferedInputStream( new FileInputStream( source ) );
			raf = new RandomAccessFile( dest,"rw" );
			
			raf.setLength( source.length() + dest.length() );
			raf.seek( source.length() );
			
			byte[] buffer = new byte[1024];
			int byteRead = 0;
			while( true ){
				byteRead = bin.read(buffer);
				if( byteRead==-1 ){
					break;
				}
				raf.write(buffer, 0, byteRead);
			}
		}catch( Exception e ){
			e.printStackTrace();
		}finally{
			if( bin!=null ){
				bin.close();
			}
			if( raf!=null ){
				raf.close();
			}
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值