JAVA文件处理工具类

package com.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class FileHanlder {
	
	/**
	 * 写入文件
	 * @param in 输入流
	 * @param out 输出流
	 * @return 成功返回真
	 * @throws IOException
	 */
	public static boolean writer(InputStream in,OutputStream out) throws IOException{
		return writer(in,out,null);
	}
	
	/**
	 * 写入文件
	 * @param in 输入流
	 * @param out 输出流
	 * @param byteSize 字节数组大小
	 * @param bufferedSzie 缓冲大小
	 * @return 成功返回真
	 * @throws IOException
	 */
	public static boolean writer(InputStream in,OutputStream out,Integer byteSize) throws IOException{
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			if (in != null && out != null) {
				if(byteSize==null){byteSize = 1024;}
				bis = new BufferedInputStream(in);
				bos = new BufferedOutputStream(out);
				byte[] bs = new byte[byteSize];
				int len = -1;
				while((len=bis.read(bs))!=-1){
					bos.write(bs, 0, len);
				}
				bos.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			if(in!=null){in.close();}
			if(out!=null){out.close();}
			if(bis!=null){bis.close();}
			if(bos!=null){bos.close();}
		}
		return true;
	}
	
	/**
	 * 读取字符文件
	 * @param path 文件路径
	 * @return String 字符串
	 */
	public static String readString(String absolutePath){
		BufferedReader read = null;
		try {
			read = new BufferedReader(new FileReader(new File(absolutePath)));
			StringBuffer sb = new StringBuffer();
			String temp  = null;
			while((temp = read.readLine()) != null){
				sb.append(temp);
				temp = read.readLine();
			}
			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {read.close();} catch (IOException e) {}
		}
		return null;
	}

	/**
	 * 读取一个对象
	 * @param path 文件路径
	 * @return Object对象
	 */
	public static Object readOjbect(String absolutePath){
		ObjectInputStream input = null;
		try {
			input = new ObjectInputStream(new FileInputStream(new File(absolutePath)));
			return input.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {input.close();} catch (IOException e) {}
		}
		return null;
	}

	/**
	 * 写入一个对象
	 * @param obj 传入一个对象
	 * @param target 文件目标位置
	 * @return 成功返回 true 失败返回 false
	 */
	public static boolean writerObject(Object obj,String absolutePath){
		ObjectOutputStream out = null;
		try {
			out = new ObjectOutputStream(new FileOutputStream(absolutePath));
			out.writeObject(obj);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {out.close();} catch (IOException e) {}
		}
		return false;
	}

	/**
	 * 写入字符文件
	 * @param content 内容
	 * @param path 文件目标路径
	 * @return 成功返回 true 失败返回 false
	 */
	public static boolean writer(String content,String path){
		return  writer(content,path,false);
	}

	/**
	 * 写入字符文件
	 * @param content 内容
	 * @param path 文件目标路径
	 * @param append 是否要在原来的文件追加
	 * @return 成功返回 true 失败返回 false
	 */
	public static boolean writer(String content,String path,boolean isAppend){
		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(path),isAppend)));
			writer.write(content);
			writer.flush();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {writer.close();} catch (IOException e) {}
		}
		return false;
	}

	/**
	 * 复制文件
	 * @param source 源文件
	 * @param target 目标位置
	 * @return 成功返回 true 失败返回 false
	 */
	public static boolean copy(String source,String target){
		InputStream input = null;
		OutputStream out = null;
		try {
			input = new FileInputStream(new File(source));
			out = new FileOutputStream(target);
			//long size = input.available();
			int len = 0;
			byte[] bytes = new byte[1024*1024*3];
			while(((len = input.read(bytes))<=0)){
				out.write(bytes,0,len);
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {input.close();out.close();} catch (Exception e2) {}
		}
		return false;
	}

	/**
	 * 剪切文件
	 * @param source 源文件
	 * @param target 目标位置
	 * @return 成功返回 true 失败返回 false
	 */
	public static boolean cut(String source,String target){
		InputStream input = null;
		OutputStream out = null;
		try {
			input = new FileInputStream(new File(source));
			out = new FileOutputStream(target);
			int len = 0;
			byte[] bytes = new byte[1024*3];
			while(((len = input.read(bytes))<=0)){
				out.write(bytes,0,len);
			}
			return new File(source).delete();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {input.close();out.close();} catch (Exception e2) {}
		}
		return false;
	}

	/**
	 * 删除文件
	 * @param path 目标位置
	 * @return 成功返回 true 失败返回 false
	 */
	public static boolean delete(String absolutePath){
		return new File(absolutePath).delete();
	} 

	/**
	 * 传入一个文件路径
	 * 获取子文件和文件夹
	 * @param path 文件路径
	 * @return String[] 文件和文件夹的路径集合
	 */
	public static String[] fileList(String path){
		File file = new File(path);
		if(file.isDirectory()){
			return file.list();
		}
		return null;
	} 
	
	/**
	 * 获取类路径下的资源文件
	 * @param fileName 文件名称
	 * @return 输入流
	 * @throws IOException
	 */
	public InputStream getClassPathInputStream(String fileName)throws IOException{
		return Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
	}
}

猜你喜欢

转载自liguanfeng.iteye.com/blog/2203674