java工具类之IO多功能类
2016-06-19 16:42
567 查看
IO相关的功能和工具类是项目中,最常见的应用之一,该工具类有多个不同的用途,请注意下边的注释。
更多学习讨论, 请加入官方QQ技术群320349384,
官方天亮论坛:http://bbs.yuqing36524.com/
天亮教育视频链接:http://pan.baidu.com/s/1pJJrcqJ
package com.letv.zel.portrait.utils; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringReader; import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Set; import org.apache.log4j.Logger; import com.letv.zel.portrait.utils.hadoop.HdfsFileOperatorUtil; /** * I/O工具类 * * @author zel * */ public class IOUtil { // 日志记录 private static Logger logger = Logger.getLogger(IOUtil.class); // 迭代读取文件或文件中的所有文本内容 public static String readDirOrFile(String filePath, String fileEncoding) { File f = new File(filePath); String temp = ""; if (f.isDirectory()) { File[] files = f.listFiles(); for (File temp_file : files) { temp += readDirOrFile(temp_file.getAbsolutePath(), fileEncoding); } return temp; } return readFile(filePath, fileEncoding); } /** * fileEncoding若为null,则采用系统默认编码 功能: 读取一个文件的文本内容 * * @param filePath * @param fileEncoding * @return */ public static String readFile(String filePath, String fileEncoding) { if (fileEncoding == null) { fileEncoding = System.getProperty("file.encoding"); } File file = new File(filePath); BufferedReader br = null; String line = null; StringBuilder sb = new StringBuilder(); try { br = new BufferedReader(new InputStreamReader(new FileInputStream( file), fileEncoding)); while ((line = br.readLine()) != null) { sb.append(line + "\n"); } return sb.toString(); } catch (Exception e) { logger.info(e.getLocalizedMessage()); } finally { if (br != null) { try { br.close(); } catch (IOException e) { logger.info(e.getLocalizedMessage()); logger.info("关闭IOUtil流时出现错误!"); } } } return null; } // 将按行读出的字符串,统一加入到set中,直接去重 public static Set<String> readFileToSet(String filePath, String fileEncoding) { if (fileEncoding == null) { fileEncoding = System.getProperty("file.encoding"); } File file = new File(filePath); BufferedReader br = null; String line = null; Set<String> lineSet = new HashSet<String>(); try { br = new BufferedReader(new InputStreamReader(new FileInputStream( file), fileEncoding)); while ((line = br.readLine()) != null) { line = line.trim(); if (StringOperatorUtil.isNotBlank(line) && (!line.startsWith("#"))) { lineSet.add(line); } } return lineSet; } catch (Exception e) { logger.info(e.getLocalizedMessage()); } finally { if (br != null) { try { br.close(); } catch (IOException e) { logger.info(e.getLocalizedMessage()); logger.info("关闭IOUtil流时出现错误!"); } } } return null; } // 按文件的开始行和结束行,部分读取文件的内容,以字符串返回 public static String readFile(String filePath, String fileEncoding, long begin_line_number, long end_line_number) { if (fileEncoding == null) { fileEncoding = System.getProperty("file.encoding"); } File file = new File(filePath); BufferedReader br = null; String line = null; StringBuilder sb = new StringBuilder(); try { br = new BufferedReader(new InputStreamReader(new FileInputStream( file), fileEncoding)); int i = 0; // 空读完前边的行 while (i < begin_line_number && (line = br.readLine()) != null) { i++; } while ((line = br.readLine()) != null && begin_line_number < end_line_number) { sb.append(line + "\n"); begin_line_number++; } // System.out.println("line---"+line); return sb.toString(); } catch (Exception e) { logger.info(e.getLocalizedMessage()); } finally { if (br != null) { try { br.close(); } catch (IOException e) { logger.info(e.getLocalizedMessage()); logger.info("关闭IOUtil流时出现错误!"); } } } return null; } // 按文件的开始行和结束行,部分读取文件的内容,以list集合返回 public static List<String> readFileToList(String filePath, String fileEncoding, long begin_line_number, long end_line_number) { if (fileEncoding == null) { fileEncoding = System.getProperty("file.encoding"); } File file = new File(filePath); BufferedReader br = null; String line = null; List<String> list = new ArrayList<String>(); try { br = new BufferedReader(new InputStreamReader(new FileInputStream( file), fileEncoding)); int i = 0; // 空读完前边的行 while (i < begin_line_number && (line = br.readLine()) != null) { i++; } while ((line = br.readLine()) != null && begin_line_number < end_line_number) { list.add(line); begin_line_number++; } } catch (Exception e) { logger.info(e.getLocalizedMessage()); } finally { if (br != null) { try { br.close(); } catch (IOException e) { logger.info(e.getLocalizedMessage()); logger.info("关闭IOUtil流时出现错误!"); } } } return list; } /** * 将一个字符串写入到一个文件 * * @param path * 储存的文件路径 * @param value * 储存的文件内容 * @throws IOException */ public static void writeFile(String path, String value, String encoding) { File f = new File(path); FileOutputStream fos = null; // 文件路径的父路径是否存在做判断 FileOperatorUtil.createParentDirFromFile(path); try { fos = new FileOutputStream(f); fos.write(value.getBytes(encoding)); fos.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } // 是否以append方式写入到文件中 public static void writeFile(String path, String value, String encoding, boolean isAppend) { File f = new File(path); FileOutputStream fos = null; // 文件路径的父路径是否存在做判断 FileOperatorUtil.createParentDirFromFile(path); try { fos = new FileOutputStream(f, isAppend); fos.write(value.getBytes(encoding)); fos.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } // 将对象写入到文件中 public static void writeFile(String path, Object obj) { File f = new File(path); FileOutputStream fos = null; byte[] byteArray = ObjectAndByteArrayConvertUtil.ObjectToByteArray(obj); try { fos = new FileOutputStream(f); fos.write(byteArray); fos.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 以追加的方式向文件中写入 * * @param path * @param value */ public static void writeFile(String path, String value, boolean isAppend, String encoding) { File f = new File(path); // 排除其父目录不存在的情况 FileOperatorUtil.createParentDirFromFile(path); FileOutputStream fos = null; try { if (isAppend) { fos = new FileOutputStream(f, isAppend); } else { fos = new FileOutputStream(f); } fos.write(value.getBytes(encoding)); fos.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } // 将字符串转化成list集合存储,以\n作为行分隔符 public static List<String> tranBatchLineToList(String source) { if (source == null || source.isEmpty()) { return null; } List<String> lineList = new LinkedList<String>(); StringReader sb = new StringReader(source); BufferedReader br = new BufferedReader(sb); String temp_line = null; try { while ((temp_line = br.readLine()) != null) { lineList.add(temp_line); } br.close(); } catch (Exception e) { e.printStackTrace(); } return lineList; } // 将文件读出,并以list对象来保存 public static List<String> getLineArrayFromFile(String path, String encoding) { String keywordString = readDirOrFile(path, encoding); List<String> lineArray = new LinkedList<String>(); try { String temp_line = null; StringReader sr = new StringReader(keywordString); BufferedReader br = new BufferedReader(sr); while ((temp_line = br.readLine()) != null) { if (!temp_line.startsWith("#")) { lineArray.add(temp_line); } } br.close(); } catch (Exception e) { e.printStackTrace(); } return lineArray; } // 得到某个文件的bufferedReader流 public static BufferedReader getBufferedReader(String filePath, String encoding) { BufferedReader br = null; try { FileInputStream fis = new FileInputStream(new File(filePath)); br = new BufferedReader(new InputStreamReader(fis, encoding)); } catch (Exception e) { e.printStackTrace(); return null; } return br; } // 得到一个文件的inputstream public static InputStream getInputStream(String filePath) { try { FileInputStream fis = new FileInputStream(new File(filePath)); return fis; } catch (Exception e) { e.printStackTrace(); } return null; } // 得到一个字符串的BufferedReader对象 public static BufferedReader getBufferedReader(String strValue) { BufferedReader br = null; try { StringReader sb = new StringReader(strValue); br = new BufferedReader(sb); } catch (Exception e) { e.printStackTrace(); return null; } return br; } // 将inputstream转化为指定encoding的字符流 public static BufferedReader convertInputStreamToBufferedReader( InputStream inStream, String encoding) { BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(inStream, encoding)); } catch (Exception e) { e.printStackTrace(); } return br; } // 关闭字符流 public static void closeReader(Reader reader) { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 读取指定文件的若干行,并转化为List集合 * * @param path * @param encoding * @param begin_number * @param line_length * @return */ public static List<String> getLineArrayFromFile(String path, String encoding, long begin_line_number, int line_length, boolean isOnlyColumn) { long end_line_number = begin_line_number + line_length; String keywordString = readFile(path, StaticValue.default_encoding, begin_line_number, end_line_number); List<String> lineArray = new LinkedList<String>(); String[] strArray = null; try { String temp_line = null; StringReader sr = new StringReader(keywordString); BufferedReader br = new BufferedReader(sr); if (isOnlyColumn) { while ((temp_line = br.readLine()) != null) { if (StringOperatorUtil.isNotBlank(temp_line)) { lineArray.add(temp_line); } } } else { while ((temp_line = br.readLine()) != null) { strArray = temp_line.split(StaticValue.separator_tab); if (strArray.length >= 2) { lineArray.add(strArray[0]); } } } br.close(); } catch (Exception e) { e.printStackTrace(); } return lineArray; } /** * 得到指定的文本的指定的行内容 * * @param path * @param encoding * @param begin_line_number * @param line_length * @param isOnlyColumn * @return */ public static List<String> getLineArrayFromFile(String path, String encoding, long begin_line_number, int line_length) { long end_line_number = begin_line_number + line_length; return readFileToList(path, encoding, begin_line_number, end_line_number); } public static List<String> tranBatchLineToList(String source, int begin_line_number, int end_line_number) { if (source == null || source.isEmpty()) { return null; } List<String> lineList = new LinkedList<String>(); StringReader sb = new StringReader(source); BufferedReader br = new BufferedReader(sb); String temp_line = null; try { int i = 0; // 空读完前边的行 while (i < begin_line_number && (temp_line = br.readLine()) != null) { i++; } while ((temp_line = br.readLine()) != null && begin_line_number < end_line_number) { lineList.add(temp_line); begin_line_number++; } br.close(); } catch (Exception e) { e.printStackTrace(); } return lineList; } // 将set对象转换为string对象,用换行符来连接 public static String tranSetToString(Set<String> strSet) { if (strSet == null || strSet.isEmpty()) { return null; } StringBuilder sb = new StringBuilder(); for (String line : strSet) { sb.append(line + StaticValue.separator_next_line); } return sb.toString(); } // 将list转换为string对象 public static String tranListToString(List list) { if (list == null || list.isEmpty()) { return null; } StringBuilder sb = new StringBuilder(); for (Object line : list) { if (line.toString().trim().length() > 0) { sb.append(line); } } return sb.toString(); } // 得到配置文件的bufferedreader流 public static BufferedReader getTextBufferedReader4ConfigFile( String configFile, String encoding) { if (StringOperatorUtil.isBlank(configFile)) { return null; } InputStream is = IOUtil.class.getClassLoader().getResourceAsStream( configFile); BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(is, encoding)); return br; } catch (Exception e) { e.printStackTrace(); } return null; } // 用读取配置文件的工具类,完成配置文件的读取 public static String readConfigFile(String filePath, String encoding) { ReadConfigUtil readConfigUtil = new ReadConfigUtil(filePath, false, encoding); return readConfigUtil.getLineConfigTxt(); } // 得到配置文件中的缓存的对象 public static Object getObjectByConfigPath(String configPath) { InputStream is = IOUtil.class.getClassLoader().getResourceAsStream( configPath); return ObjectAndByteArrayConvertUtil.readObjectFromInputStream(is); } // 从HDFS中读取对象 public static Object getObjectByHDFS(String hdfsPath) { InputStream is = HdfsFileOperatorUtil.getFSDataInputStream(hdfsPath); return ObjectAndByteArrayConvertUtil.readObjectFromInputStream(is); } // 从配置文件中得到inputstream public static InputStream getInputStreamByConfigPath(String configPath) { InputStream is = IOUtil.class.getClassLoader().getResourceAsStream( configPath); return is; } // 从文件对象读取出缓存的对象 public static Object getObjectByFileSystem(String filePath) { InputStream is = IOUtil.getInputStream(filePath); return ObjectAndByteArrayConvertUtil.readObjectFromInputStream(is); } public static void main(String[] args) throws Exception { // String source=readFile("resource/library.dic",null); // String source = readFile(ReadConfigUtil.getValue("dic.path"), null); // String source = readDirOrFile("d://temp", "gbk"); // System.out.println(source); String source_string = IOUtil.readFile("d:\\test\\new_words2.txt", "utf-8"); StringReader sr = new StringReader(source_string); BufferedReader br = new BufferedReader(sr); String temp = null; StringBuilder sb = new StringBuilder(); HashSet<String> hashSet = new HashSet<String>(); while ((temp = br.readLine()) != null) { if (temp.trim().length() > 1 && temp.trim().length() <= 4) { if (!hashSet.contains(temp)) { sb.append(temp + "\n"); } else { hashSet.add(temp); } } } IOUtil.writeFile("d:\\test\\new_words3.txt", sb.toString(), StaticValue.default_encoding); } }
更多学习讨论, 请加入官方QQ技术群320349384,
官方天亮论坛:http://bbs.yuqing36524.com/
天亮教育视频链接:http://pan.baidu.com/s/1pJJrcqJ
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 简单易懂云计算(转自天涯感谢原楼主iamsatisfied)
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序