Java 简单IO文件处理

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本人声明。否则将追究法律责任。
作者: 永恒の_☆ 地址: http://blog.csdn.net/chenghui0317/article/details/8188230
package com.java.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;

/**
 * operate file
 * @author Administrator
 *
 */
public class OperateFile {

	/**
	 * create this file 
	 * @param filePath
	 * @throws IOException 
	 */
	public static void createFile(String filePath) throws IOException{
		int lastPoint = filePath.lastIndexOf("\\");
		if(lastPoint==-1){
			throw new IOException("this filePath is not true !!");
		}
		String folderPath = filePath.substring(0, lastPoint);
		File folderFile = new File(folderPath);
		if(folderFile.exists()){
			File newFile = new File(filePath);
			if(!newFile.exists()){
				newFile.createNewFile();
			}else{
				throw new IOException("the file is exists !!");
			}
		}else{
			throw new IOException("application cann't find the folder!!"); 
		}
		System.out.println("execute createFile(filePath) success!!");
	} 
	
	/**
	 * delete this file
	 * @param filePath
	 * @throws IOException
	 */
	public static void deleteFile(String filePath) throws IOException{
		String folderPath = filePath.substring(0, filePath.lastIndexOf("\\"));
		File folderFile = new File(folderPath);
		if(folderFile.exists()){
			File file = new File(filePath);
			if(file.exists()){
				file.delete();
			}else{
				throw new IOException("the file is not exists !!");
			}
		}else{
			throw new IOException("application cann't find the folder!!"); 
		}
		System.out.println("execute deleteFile(filePath) success!!");
	}
	
	/**
	 * copy file to target file
	 * @param srcPath
	 * @param targetPath
	 * @throws IOException 
	 */
	public static void copyFile(String srcPath, String targetPath) throws IOException{
		File srcFile = new File(srcPath);
		if(srcFile.exists()){
			//create fileInputDtream read file
			FileInputStream fileInputStream = new FileInputStream(srcFile);
			File targetFile = new File(targetPath);
			if(!targetFile.exists()){
				createFile(targetPath);
			}
			//create fileOutputStream write file
			FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
			int content = fileInputStream.read();
			while(content!=-1){
				fileOutputStream.write(content);
				content = fileInputStream.read();
			}
			fileOutputStream.flush();
			
			fileOutputStream.close();
			fileInputStream.close();
			
			System.out.println("execute copyFile(srcPath, targetPath) success!!");
		}else {
			throw new IOException("the src file is not exists!!");
		}
	}
	
	/**
	 * copy file to target file by buffered
	 * @param srcPath
	 * @param targetPath
	 * @throws IOException
	 */
	public static void copyFileByBuffered(String srcPath, String targetPath) throws IOException{
		File srcFile = new File(srcPath);
		if(srcFile.exists()){
			//create fileInputDtream read file
			FileInputStream fileInputStream = new FileInputStream(srcFile);
			//create bufferedInputStream 
			BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
			File targetFile = new File(targetPath);
			if(!targetFile.exists()){
				createFile(targetPath);
			}
			//create fileOutputStream write file
			FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
			//create bufferedOutputStream
			BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
			
			int content = bufferedInputStream.read();
			while(content!=-1){
				bufferedOutputStream.write(content);
				content = bufferedInputStream.read();
			}
			fileOutputStream.flush();
			
			bufferedOutputStream.close();
			bufferedInputStream.close();
			
			fileOutputStream.close();
			fileInputStream.close();
			System.out.println("execute copyFileByBuffered(srcPath, targetPath) success!!");
		}else {
			throw new IOException("the src file is not exists!!");
		}
	}
	
	/**
	 * copy file to target file by buffered ,then delete the file
	 * @param srcPath
	 * @param targetPath
	 * @throws IOException
	 */
	public static void cutFileByBuffered(String srcPath, String targetPath) throws IOException{
		copyFileByBuffered(srcPath, targetPath);
		deleteFile(srcPath);
		System.out.println("execute cutFileByBuffered(srcPath, targetPath) success!!");
	}
	
	/**
	 * create this folder
	 * @param folderPath
	 * @throws IOException 
	 */
	public static void createFolder(String folderPath) throws IOException{
		File folderFile = new File(folderPath);
		if(!folderFile.exists()){
			//folderFile.mkdir();
			//mkdirs 会创建指定路径的多个不存在文件夹,但是mkdir只会创建最底层的文件夹,如果其父目录不存在 则什么都不做
			folderFile.mkdirs();  
		}else{
			throw new IOException("this folder is exists!!!");
		}
		System.out.println("execute createFolder(folderPath) success!!");
	}
	
	/**
	 * copy folder to target folder 
	 * @param srcFolderPath
	 * @param targetFolderPath
	 * @throws IOException
	 */
	public static void copyFolder(String srcFolderPath, String targetFolderPath) throws IOException{
		File srcFolder = new File(srcFolderPath);
		if(srcFolder.exists()){			
			createFolder(targetFolderPath);
			String [] folderStrs = srcFolder.list();
			for (int i = 0; i < folderStrs.length; i++) {
				String subSrcFolderPath = srcFolderPath  + File.separator + folderStrs[i];
				File subSrcFolder = new File(subSrcFolderPath);
				
				String subTargetFilePath = targetFolderPath + File.separator + folderStrs[i];
				if(subSrcFolder.isDirectory()){
					copyFolder(subSrcFolderPath, subTargetFilePath);
				}else{
					copyFileByBuffered(subSrcFolderPath, subTargetFilePath);
				}
			}
		}else{
			throw new IOException("this src folder is not exists!!!");
		}
		System.out.println("execute copyFolder(srcFolderPath, String targetFolderPath) success!!");
	}
	
	/**
	 * delete this folder
	 * @param folderPath
	 * @throws IOException
	 */
	public static void deleteFolder(String folderPath) throws IOException{
		File folderFolder = new File(folderPath);
		if(folderFolder.exists()){			
			 String [] folderStrs = folderFolder.list();
			 for (int i = 0; i < folderStrs.length; i++) {
				String subFolderPath = folderPath + File.separator + folderStrs[i];
				File subFolder = new File(subFolderPath);
				if(subFolder.isDirectory()){
					deleteFolder(subFolderPath);
				}else{
					subFolder.delete();
				}
			}
			 folderFolder.delete();
		}else{
			throw new IOException("this src folder is not exists!!!");
		}
		System.out.println("execute deleteFolder(folderPath) success!!");
	}
	
	public static void main(String[] args) {
		try {
			//OperateFile.deleteFile("D:\\a");
			//OperateFile.copyFile("D:\\a1.txt", "D:\\b.txt");
			//OperateFile.copyFileByBuffered("D:\\a.txt", "D:\\b.txt");
			//OperateFile.cutFileByBuffered("D:\\a.txt", "D:\\b.txt");
			//OperateFile.createFolder("D:\\aaa.txt");
			//OperateFile.copyFolder("d:\\a","d:\\newa");
			OperateFile.deleteFolder("d:\\newa");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值