检索中文工具(SearchZhUtil)3

来源:互联网 发布:淘宝网络电话 编辑:程序博客网 时间:2024/06/06 04:48

 

/**
  * 读取文件,使用list返回文件内容,文件中的每一行就是list 的一个数据
  *
  * @param fileName
  * @return List<String>
  */
 public static List<String> readFile(String fileName) {
  if (fileName == null || "".equals(fileName.trim())) {
   return null;
  }
  fileName = fileName.trim();
  BufferedReader reader = null;
  List<String> contentList = null;
  try {
   reader = new BufferedReader(new InputStreamReader(
     new FileInputStream(fileName), "utf-8"));
   String content = null;
   contentList = new ArrayList<String>();
   while ((content = reader.readLine()) != null) {
    contentList.add(content);
   }
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   try {
    reader.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }

  return contentList;
 }

 /**
  * 写文件,每一个user是一条记录
  *
  * @param user
  * @return boolean
  */
 public static boolean writeFile(List<String> contents, String filePath) {
  boolean isSuccess = false;
  if (contents == null || contents.isEmpty()) {
   return isSuccess;
  }
  if (filePath == null || "".equals(filePath.trim())) {
   return isSuccess;
  }
  filePath = filePath.trim();

  BufferedWriter writer = null;

  try {
   writer = new BufferedWriter(new OutputStreamWriter(
     new FileOutputStream(filePath, true), "UTF-8"));
   int count = 0;
   int MAX = 500;
   for (String content : contents) {
    writer.write(content);
    writer.write("/t/n");
    count++;
    if (count >= MAX) {
     writer.flush();
     count = 0;
    }
   }
   writer.flush();
   isSuccess = true;
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try {
    writer.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  return isSuccess;
 }

 

原创粉丝点击