IO操作工具类--基于common-io IOjava
程序员文章站
2022-03-22 14:17:03
...
封装IO的一些常用操作:基于Common-io :IOUtils方法
CommonUtils.java
CommonUtils.java
package com.gbcom.frame.io; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.util.ArrayList; import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; /** * 封装IO的一些常用操作:基于Common-io :IOUtils方法 * 1.read.write.copy.delete.convert * * * 无异常 无空指针 * @author SYZ * @date 2014-12-4,上午10:20:22 * @version v1.0.0 * @see com.gbcom.frame.io.CommonUtils */ public class CommonUtils { private static List<String> readLines(File file) throws IOException{ return readLines(file,"UTF-8"); } private static List<String> readLines(File file,String encoding) throws IOException{ return FileUtils.readLines(file, encoding); } private static List<String> readLines(InputStream input) throws IOException{ return readLines(input,"UTF-8"); } private static List<String> readLines(InputStream input,String encoding) throws IOException{ return IOUtils.readLines(input, encoding); } private static List<String> readLines(Reader read) throws IOException{ return IOUtils.readLines(read); } /** * 读取文件,保存到byte数组中:不需要指定具体编码格式 * @param srcFile * @return */ public static byte[] read(String srcFile){ File file = new File(srcFile); InputStream input = null; try{ input = new FileInputStream(file); return IOUtils.toByteArray(input); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ IOUtils.closeQuietly(input); } return new byte[0]; } /** * 读取文件:保存到String对象中,默认使用utf-8作为编码格式 * @param srcFile * @return */ public static String readAll(String srcFile){ File file = new File(srcFile); InputStream input = null; try{ input = new FileInputStream(file); return IOUtils.toString(input, "utf-8"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ IOUtils.closeQuietly(input); } return ""; } /** * 读取文件,按行分割保存到list中,默认编码格式为utf-8 * @param srcFile * @return */ public static List<String> readLines(String srcFile){ File file = new File(srcFile); InputStream input = null; try { input = new FileInputStream(file); return readLines(input,"utf-8"); //IOUtils.readLines(input, encoding); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ IOUtils.closeQuietly(input); } return new ArrayList<String>(0); } /** * 写文件。最原始的方式,支持 byte数组,无序提供编码格式 * @param srcFile * @param data */ public static void wirte(String srcFile,byte[] data){ File file = new File(srcFile); OutputStream out = null; try { out = new FileOutputStream(file); IOUtils.write(data, out); } catch (IOException e) { e.printStackTrace(); }finally{ if(out !=null){ IOUtils.closeQuietly(out); } } } /** * 一次性写入文件,如果需要分行,需要再buffer中使用\n 实现 * @param srcFile * @param buffer */ public static void writeAll(String srcFile,StringBuffer buffer){ File file = new File(srcFile); OutputStream out = null; try { out = new FileOutputStream(file); IOUtils.write(buffer, out,"utf-8"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(out !=null){ IOUtils.closeQuietly(out); } } } /** * 写文件:支持多行写入,list提供 * @param srcFile * @param lines */ public static void writeLines(String srcFile,List<String> lines){ File file = new File(srcFile); OutputStream out = null; try { out = new FileOutputStream(file); IOUtils.writeLines(lines, null, out, "UTF-8"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(out !=null){ IOUtils.closeQuietly(out); } } } /** * 在文件末尾添加:仅支持 * @param scrFile * @param Lines */ public static void appendWrite(String srcFile,List<String>lines){ File file = new File(srcFile); OutputStream out = null; try { out = new FileOutputStream(file,true);//append IOUtils.writeLines(lines, null, out, "UTF-8"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(out !=null){ IOUtils.closeQuietly(out); } } } /** * 在文件末尾添加:仅支持 List 和 StringBuffer 其他省略 * @param scrFile * @param Lines */ public static void appendWrite(String srcFile,StringBuffer buffer ){ File file = new File(srcFile); OutputStream out = null; try { out = new FileOutputStream(file,true);//append IOUtils.write(buffer, out, "UTF-8"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(out !=null){ IOUtils.closeQuietly(out); } } } /** * 拷贝文件,,return count failed return -1 * @param src * @param dest */ public static int copy(String src,String dest){ File srcFile = new File(src); File destFile = new File(dest); InputStream input = null; OutputStream output = null; try { input = FileUtils.openInputStream(srcFile); output = new FileOutputStream(destFile); return IOUtils.copy(input, output); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(output !=null){ IOUtils.closeQuietly(output); } if(input !=null){ IOUtils.closeQuietly(input); } } return -1; } /** * 支持文件夹操作 * @param dest */ public static void delete(String ... dests){ for(String dest : dests){ File file = new File(dest); try { FileUtils.forceDeleteOnExit(file); } catch (IOException e) { e.printStackTrace(); continue; }finally{ file = null; } } } public static void main(String args []) throws IOException{ String dir = "d:\\"; String srcName = "xxx.jpg"; String destName = "test.xds"; // FileUtils.forceMkdir(new File(dir)); // // copy(dir+File.separator+srcName,dir+File.separator+destName); delete(dir+File.separator+srcName,dir+File.separator+destName); } }
上一篇: svn导出某版本号或某日期后修改的文件
下一篇: 值得一读的Java书籍,你读过几本?