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);
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import java.io.*; /** * FileUtil. Simple file operation class. * * @author BeanSoft * */ public class FileUtil { /** * The buffer. */ protected static byte buf[] = new byte[1024]; /** * Read content from local file. FIXME How to judge UTF-8 and GBK, the * correct code should be: FileReader fr = new FileReader(new * InputStreamReader(fileName, "ENCODING")); Might let the user select the * encoding would be a better idea. While reading UTF-8 files, the content * is bad when saved out. * * @param fileName - * local file name to read * @return * @throws Exception */ public static String readFileAsString(String fileName) throws Exception { String content = new String(readFileBinary(fileName)); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(String fileName, String encoding) throws Exception { String content = new String(readFileBinary(fileName), encoding); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(InputStream in) throws Exception { String content = new String(readFileBinary(in)); return content; } /** * Read content from local file to binary byte array. * * @param fileName - * local file name to read * @return * @throws Exception */ public static byte[] readFileBinary(String fileName) throws Exception { FileInputStream fin = new FileInputStream(fileName); return readFileBinary(fin); } /** * 从输入流读取数据为二进制字节数组. * @param streamIn * @return * @throws IOException */ public static byte[] readFileBinary(InputStream streamIn) throws IOException { BufferedInputStream in = new BufferedInputStream(streamIn); ByteArrayOutputStream out = new ByteArrayOutputStream(10240); int len; while ((len
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值