java file操作工具类

package com.test.web.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;

public class FileUtils {

	/**
	 * 得到路径下对应所有的文件及文件夹
	 * 
	 * @param path
	 * @return
	 */
	public static File[] fileList(String path) {
		File file = new File(path);
		return file.listFiles();
	}

	/**
	 * 得到路径下符合条件的文件及文件夹
	 * 
	 * @param path
	 * @param cond
	 * @return
	 */
	public static File[] fileList(String path, final String cond) {
		File file = new File(path);
		return file.listFiles(new FilenameFilter() {
			public boolean accept(File dir, String name) {
				return name.indexOf(cond) >= 0;
			}

		});
	}

	/**
	 * 读取文件
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static String readFile(String path) throws IOException {
		File file = new File(path);
		StringBuffer strBuffer = new StringBuffer();
		Reader reader = null;
		BufferedReader breader = null;
		try {
			reader = new FileReader(file);
			breader = new BufferedReader(reader);
			String strline = null;
			while ((strline = breader.readLine()) != null) {
				strBuffer.append(strline + "\n");
			}
		} catch (FileNotFoundException e) {
			throw new FileNotFoundException("文件不能被发现!");
		} catch (IOException e) {
			throw new IOException("文件读取中出现了异常!");
		} finally {
			if (breader != null)
				breader.close();
			if (reader != null)
				reader.close();
		}
		return strBuffer.toString();
	}
	
	/**
	 * 写文件
	 * @param path
	 * @param content
	 * @throws IOException
	 */
    public static void writeFile(String path , String content) throws IOException{
	    File file = new File(path);
	    Writer writer = null;
	    BufferedWriter bwriter = null;
	    PrintWriter out = null;
	    try {
			writer = new FileWriter(file , true);
			bwriter = new BufferedWriter(writer);
			out = new PrintWriter(bwriter);
			out.println(content);
		} catch (IOException e) {
			throw new IOException("写文件出现了异常!");
		} finally {
			if(out != null) out.close();
			if(bwriter != null) bwriter.close();
			if(writer != null) writer.close();
		}
	}
	/**
	 * 创建文件
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static boolean createFile(String path) throws IOException{
		File file = new File(path);
		try {
			return file.createNewFile();
		} catch (IOException e) {
			throw new IOException("文件创建失败!");
		}
	}
	/**
	 * 创建多个文件夹
	 * @param path
	 * @return
	 */
	public static boolean mkdirs(String path){
		File file = new File(path);
	    return file.mkdirs();
	}
	
	/**
	 * 更换名称
	 * @param oldpath 
	 * @param newfname
	 * @return
	 */
	public static boolean renameFile(String oldpath , String newfname){
		File oldfile = new File(oldpath);
		File newfile = new File(oldpath.substring(0, oldpath.indexOf("\\") + 1) + newfname);
		return oldfile.renameTo(newfile);
	}
	
	/**
	 * 删除文件及文件夹
	 * @param path
	 * @return
	 */
	public static boolean delete(String path){
		File file = new File(path);
	    return file.delete();
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值