com.hankcs.hanlp.corpus.io
类 IOUtil

java.lang.Object
  继承者 com.hankcs.hanlp.corpus.io.IOUtil

public class IOUtil
extends Object

一些常用的IO操作

作者:
hankcs

嵌套类摘要
static class IOUtil.LineIterator
          方便读取按行读取大文件
 
构造方法摘要
IOUtil()
           
 
方法摘要
static String dirname(String path)
          获取文件所在目录的路径
static byte[] getBytes(InputStream is)
           
static InputStream getInputStream(String path)
          智能获取InputStream,如果是资源文件则返回相应的InputStream
static String getSuffix(String name, String delimiter)
          获取最后一个分隔符的后缀
static boolean isFileExists(String path)
          判断文件是否存在
static boolean isResource(String path)
          判断资源是否位于jar中
static void loadDictionary(BufferedReader br, TreeMap<String,CoreDictionary.Attribute> storage)
          将一个BufferedReader中的词条加载到词典
static TreeMap<String,CoreDictionary.Attribute> loadDictionary(String... pathArray)
          加载词典,词典必须遵守HanLP核心词典格式
static BufferedReader newBufferedReader(String path)
          创建一个BufferedReader
static BufferedWriter newBufferedWriter(String path)
          创建一个BufferedWriter
static BufferedWriter newBufferedWriter(String path, boolean append)
           
static byte[] readBytes(String path)
          将整个文件读取为字节数组
static byte[] readBytesFromResource(String path)
          将资源中的一个资源读入byte数组
static LinkedList<String[]> readCsv(String path)
           
static IOUtil.LineIterator readLine(String path)
           
static LinkedList<String> readLineList(String path)
           
static LinkedList<String> readLineListWithLessMemory(String path)
          用省内存的方式读取大文件
static Object readObjectFrom(String path)
          反序列化对象
static String readTxt(String path)
          一次性读入纯文本
static
<T> boolean
saveCollectionToTxt(Collection<T> collection, String path)
           
static boolean saveEntrySetToTxt(Set<Map.Entry<Object,Object>> entrySet, String path, String separator)
           
static boolean saveMapToTxt(Map<Object,Object> map, String path)
           
static boolean saveMapToTxt(Map<Object,Object> map, String path, String separator)
           
static boolean saveObjectTo(Object o, String path)
          序列化对象
static boolean saveTxt(String path, String content)
          快速保存
static boolean saveTxt(String path, StringBuilder content)
           
static void writeLine(BufferedWriter bw, String... params)
          写数组,用制表符分割
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

构造方法详细信息

IOUtil

public IOUtil()
方法详细信息

saveObjectTo

public static boolean saveObjectTo(Object o,
                                   String path)
序列化对象

参数:
o -
path -
返回:

readObjectFrom

public static Object readObjectFrom(String path)
反序列化对象

参数:
path -
返回:

readTxt

public static String readTxt(String path)
一次性读入纯文本

参数:
path -
返回:

readCsv

public static LinkedList<String[]> readCsv(String path)

saveTxt

public static boolean saveTxt(String path,
                              String content)
快速保存

参数:
path -
content -
返回:

saveTxt

public static boolean saveTxt(String path,
                              StringBuilder content)

saveCollectionToTxt

public static <T> boolean saveCollectionToTxt(Collection<T> collection,
                                              String path)

readBytes

public static byte[] readBytes(String path)
将整个文件读取为字节数组

参数:
path -
返回:

readBytesFromResource

public static byte[] readBytesFromResource(String path)
                                    throws IOException
将资源中的一个资源读入byte数组

参数:
path -
返回:
抛出:
IOException

getBytes

public static byte[] getBytes(InputStream is)
                       throws IOException
抛出:
IOException

readLineList

public static LinkedList<String> readLineList(String path)

readLineListWithLessMemory

public static LinkedList<String> readLineListWithLessMemory(String path)
用省内存的方式读取大文件

参数:
path -
返回:

saveMapToTxt

public static boolean saveMapToTxt(Map<Object,Object> map,
                                   String path)

saveMapToTxt

public static boolean saveMapToTxt(Map<Object,Object> map,
                                   String path,
                                   String separator)

saveEntrySetToTxt

public static boolean saveEntrySetToTxt(Set<Map.Entry<Object,Object>> entrySet,
                                        String path,
                                        String separator)

dirname

public static String dirname(String path)
获取文件所在目录的路径

参数:
path -
返回:

readLine

public static IOUtil.LineIterator readLine(String path)

isFileExists

public static boolean isFileExists(String path)
判断文件是否存在

参数:
path -
返回:

isResource

public static boolean isResource(String path)
判断资源是否位于jar中

参数:
path -
返回:

getInputStream

public static InputStream getInputStream(String path)
                                  throws FileNotFoundException
智能获取InputStream,如果是资源文件则返回相应的InputStream

参数:
path -
返回:
抛出:
FileNotFoundException

newBufferedWriter

public static BufferedWriter newBufferedWriter(String path)
                                        throws FileNotFoundException,
                                               UnsupportedEncodingException
创建一个BufferedWriter

参数:
path -
返回:
抛出:
FileNotFoundException
UnsupportedEncodingException

newBufferedReader

public static BufferedReader newBufferedReader(String path)
                                        throws FileNotFoundException,
                                               UnsupportedEncodingException
创建一个BufferedReader

参数:
path -
返回:
抛出:
FileNotFoundException
UnsupportedEncodingException

newBufferedWriter

public static BufferedWriter newBufferedWriter(String path,
                                               boolean append)
                                        throws FileNotFoundException,
                                               UnsupportedEncodingException
抛出:
FileNotFoundException
UnsupportedEncodingException

getSuffix

public static String getSuffix(String name,
                               String delimiter)
获取最后一个分隔符的后缀

参数:
name -
delimiter -
返回:

writeLine

public static void writeLine(BufferedWriter bw,
                             String... params)
                      throws IOException
写数组,用制表符分割

参数:
bw -
params -
抛出:
IOException

loadDictionary

public static TreeMap<String,CoreDictionary.Attribute> loadDictionary(String... pathArray)
                                                               throws IOException
加载词典,词典必须遵守HanLP核心词典格式

参数:
pathArray - 词典路径,可以有任意个
返回:
一个储存了词条的map
抛出:
IOException - 异常表示加载失败

loadDictionary

public static void loadDictionary(BufferedReader br,
                                  TreeMap<String,CoreDictionary.Attribute> storage)
                           throws IOException
将一个BufferedReader中的词条加载到词典

参数:
br - 源
storage - 储存位置
抛出:
IOException - 异常表示加载失败


Copyright © 2014–2015 鐮佸啘鍦�/a>. All rights reserved.