java file操作工具类
2012-12-08 16:05
162 查看
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(); } }
相关文章推荐
- java文件操作工具类分享(file文件工具类)
- java IO 字节流、字符流操作总结一之File类
- Java操作集合的工具类
- Java操作文件夹的工具类
- Java基础回顾_IO流_File简单操作
- java 数据库操作工具类
- Java:IO操作工具类
- [Java 12 IO] File 类 的几个基本操作
- java操作Json工具类
- java第八章集合中的Collections操作集合的工具类
- java File 类常用操作,和java 文件流常用操作演示
- BufferedReader + FileReader读取文件并打印出来每一行(Java文件操作)
- 关于日期时间操作工具类DateUtil(二) -----对java.sql.Timestamp 的操作.
- JAVA核心技术学习——流与文件(5.操作文件FILE)
- java io 字符流操作工具类
- Java IO操作——文件操作类File的使用
- log4j FileWatchdog工具类,用来监控文件,如有变动就执行特定的操作
- JAVA本地程序使用FileSystemXmlApplicationContext读取application-datasorce.xml进行数据库操作
- Java日期操作工具类
- Java篇-File类之常用操作