开发工具类-简单文件操作相关
与文件简单操作相关的一个工具类
package org.dennisit.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; /** * Description: Simple File Handler Util , This Util rely on class<code>Logger</code> in package porg.apache.log4j. * * @author <a href='mailto:dennisit@163.com'>Cn.苏若年(En.dennisit)</a> Copy Right since 2013-9-5 * * com.parser.util.FileUtil.java * */ public class FileUtil { public static final Logger log = Logger.getLogger(FileUtil.class); /** * * 创建文件,如果文件所在的目录不存在,就创建目录,然后床架文件 * * @author <a href='mailto:dennisit@163.com'>Cn.苏若年(En.dennisit)</a> Copy Right since 2013-9-14 上午09:46:29 * * @param fileName 待创建的文件名 * @return 文件是否创建成功 */ public static boolean createFile(String fileName){ File file = new File(fileName); //判断目录是否存在 if(!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } //判断文件是否存在 if(file.exists()){ return true; } try{ if(file.createNewFile()){ return true; }else{ return false; } }catch(Exception e){ log.info("[File Create Error]",e); return false; } } /** * 根据串值对象,创建文件才 * * @author <a href='mailto:dennisit@163.com'>Cn.苏若年(En.dennisit)</a> Copy Right since 2013-9-14 上午09:48:51 * * @param fileName * 文件名 * @param str * 要写入的文件内容 * @param append * <dl>文件写入的方式,追加还是替换 * <dd>true值表示在原来内容的基础上记性内容追加</dd> * <dd>false值表示清空原来内容,重新写入</dd> * </dl> * @return */ public static boolean createFile(String fileName,String str,boolean append){ boolean ret = false; try{ if(createFile(fileName)){ FileOutputStream fileOutputStream = new FileOutputStream(fileName,append); fileOutputStream.write(str.getBytes()); fileOutputStream.close(); ret = true; } }catch (Exception e){ log.info("[File Write Error]\t" + str, e ); ret = false; } return ret; } /** * * 根据流创建文件 * * @author <a href='mailto:dennisit@163.com'>Cn.苏若年(En.dennisit)</a> Copy Right since 2013-9-14 上午09:53:00 * * @param fileName * 文件名称 * @param inputStream * 输入流 * @param append <dl>文件写入的方式,追加还是替换 * <dd>true值表示在原来内容的基础上记性内容追加</dd> * <dd>false值表示清空原来内容,重新写入</dd> * </dl> * @return */ public static boolean createFile(String fileName, InputStream inputStream,boolean append){ boolean ret = false; try{ if(createFile(fileName)){ FileOutputStream fileOutputStream = new FileOutputStream(fileName,append); byte[] buffer = new byte[4096]; int n_bytes; while ( (n_bytes = inputStream.read(buffer)) != -1 ) { fileOutputStream.write(buffer, 0, n_bytes); fileOutputStream.flush(); } fileOutputStream.close(); ret = true; } }catch (Exception e){ log.info("文件写入失败:" + fileName, e ); ret = false; } return ret; } /** * * 根据文件路径获取路径下的文件名 * * @author <a href='mailto:dennisit@163.com'>Cn.苏若年(En.dennisit)</a> Copy Right since 2013-9-14 上午09:54:35 * * @param path * @return 返回文件夹/文件名的list集合,只是文件名,不包含路径 * @throws Exception */ public static List<String> fileNameList(String path) throws Exception{ List<String> list = new ArrayList<String>(); File file = new File(path); if( file == null || !file.exists() ) throw new Exception("File (" + path + ") is not Found"); for ( File f : file.listFiles() ) list.add(f.getName()); return list; } /** * * 复制文件功能 * * @author <a href='mailto:dennisit@163.com'>Cn.苏若年(En.dennisit)</a> Copy Right since 2013-9-14 上午10:08:17 * * @param originalFileName * 原始文件路径 * @param destinationFileName * 目标文件路径 * @return */ public static boolean copyFile(String originalFileName,String destinationFileName){ boolean ret = true; if(originalFileName == null || destinationFileName == null){ ret = false; } try{ File sourceFile = new File(originalFileName); File destinationFile = new File(destinationFileName); //判断源文件是否存在 if(!sourceFile.exists()){ log.info("Original File (" + originalFileName + ") Nod Found ." ); return false; } //创建目标文件 if(!createFile(destinationFileName)){ log.info("Target File (" + destinationFileName +") Create Error ." ); return false; } if(copySingleFile(sourceFile, destinationFile)){ log.info("Copy File (" + originalFileName + ") to (" + destinationFileName +")"); }else{ ret = false; log.info("Copy File (" + originalFileName + ") to (" + destinationFileName +") ERROR "); } }catch (Exception e){ log.info("Copy File ERROR ! File Name is : " + originalFileName, e ); ret = false; } return ret; } /** * * 复制文件的实际处理类 * * @author <a href='mailto:dennisit@163.com'>Cn.苏若年(En.dennisit)</a> Copy Right since 2013-9-14 上午10:08:17 * * @param sourceFileName * 文件原来名称 * @param destinationFileName * 目标文件名称 * @return */ private static boolean copySingleFile(File sourceFile,File destinationFile){ boolean copyFlg = false; FileInputStream fileInputStream = null; DataInputStream dataInputStream = null; FileOutputStream fileOutputStream = null; DataOutputStream dataOutputStream = null; try{ fileInputStream = new FileInputStream(sourceFile); dataInputStream = new DataInputStream(new BufferedInputStream(fileInputStream)); fileOutputStream = new FileOutputStream(destinationFile); dataOutputStream = new DataOutputStream(new BufferedOutputStream(fileOutputStream)); byte[] b = new byte[1024]; int len; while((len = dataInputStream.read(b)) != -1){ dataOutputStream.write(b,0,len); } dataOutputStream.flush(); }catch(Exception e){ log.info("File Copy ERROR",e); }finally{ try{ if(dataInputStream != null){ dataInputStream.close(); } if(dataOutputStream != null){ dataOutputStream.close(); } }catch (Exception e){ } } //判断文件大小是否相同 if(sourceFile.length() == destinationFile.length()){ copyFlg = true; } return copyFlg; } /** * 删除文件 * * @author <a href='mailto:dennisit@163.com'>Cn.苏若年(En.dennisit)</a> Copy Right since 2013-9-14 上午10:10:22 * * @param fileName * @return */ public boolean deleteFile(String fileName){ File file = new File(fileName); if(!file.exists()){ log.info("Delete File Not Found :" + fileName ); return false; } try{ file.delete(); }catch(Exception e){ log.info("Delete File Error",e); return false; } return true; } public static void main(String[] args) throws Exception { copyFile("F:/BaiDu Yum/苏若年-HtmlParser篇Filter应用说明.pdf", "D:/test.pdf"); createFile("F:/tmp/test.txt"); createFile("F:/tmp/test.txt","simple File Util", true); } }
热爱生活,热爱Coding,敢于挑战,用于探索 ...