Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1378294
  • 博文数量: 187
  • 博客积分: 10375
  • 博客等级: 上将
  • 技术积分: 3127
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-07 10:58
文章分类

全部博文(187)

文章存档

2013年(1)

2012年(8)

2011年(28)

2010年(36)

2009年(47)

2008年(67)

我的朋友

分类: Java

2012-06-15 15:19:09

简单写的代码, 方便提取日文文字.


点击(此处)折叠或打开

  1. package com.test;

  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.regex.Matcher;
  10. import java.util.regex.Pattern;

  11. public class GetJPText {


  12.     static String filePath = "F:\\proj\\src";
  13.     static File resultFile = new File("F:\\result-js.txt");

  14.     /**
  15.      * @param args
  16.      */
  17.     public static void main(String[] args) throws Exception {
  18.         GetJPText.exec();
  19.     }

  20.     // Check exist JP text

  21.     public static boolean checkJPTextExist(String s) {
  22.         if (null == s || s.length() < 1) {
  23.             return false;
  24.         }
  25.         for (int i = 0; i < s.length(); i++) {
  26.             if (s.charAt(i) > 256 && (int) s.charAt(i) != 65279) {
  27.                 System.out.println((int) s.charAt(i));
  28.                 return true;
  29.             }
  30.         }
  31.         return false;
  32.     }

  33.     // Comment line check

  34.     public static boolean checkCommentLine(String lineStr) {

  35.         Pattern pattern = Pattern.compile("^(//|/\\*|\\*|trace).+");
  36.         Matcher m = pattern.matcher(lineStr);
  37.         if (m.matches()) {
  38.             return true;
  39.         }

  40.         return false;
  41.     }

  42.     public static void write2File(int line, String filename, String lineStr) throws Exception {
  43.         BufferedWriter out = new BufferedWriter(new FileWriter(resultFile, true));
  44.         // for()

  45.         out.write(line + "\t" + filename + "\t" + lineStr);
  46.         out.newLine();
  47.         out.close();
  48.         out = null;
  49.     }

  50.     // Read file

  51.     public static void readFileLine(File file) {
  52.         BufferedReader reader = null;
  53.         try {
  54.             reader = new BufferedReader(new FileReader(file));
  55.             String tmpStr = null;
  56.             int line = 1;

  57.             while ((tmpStr = reader.readLine()) != null) {
  58.                 tmpStr = tmpStr.replaceAll("\\t", " ");
  59.                 tmpStr = tmpStr.trim();
  60.                 if (!checkCommentLine(tmpStr)) {
  61.                     if (checkJPTextExist(tmpStr)) {
  62.                         write2File(line, file.getPath(), tmpStr);
  63.                     }
  64.                 }
  65.                 line++;
  66.             }

  67.             reader.close();
  68.         } catch (Exception e) {
  69.             e.printStackTrace();
  70.         } finally {
  71.             if (reader != null) {
  72.                 try {
  73.                     reader.close();
  74.                 } catch (Exception e) {
  75.                     e.printStackTrace();
  76.                 }
  77.             }
  78.         }
  79.     }

  80.     //

  81.     public static void exec() throws Exception {
  82.         List<File> listf = new ArrayList<File>();
  83.         try {
  84.             GetJPText.getAsFileList(listf, new File(filePath));
  85.         } catch (Exception e) {
  86.             e.printStackTrace();
  87.         }
  88.         System.out.println("List size: " + listf.size());

  89.         for (File file : listf) {
  90.             readFileLine(file);
  91.         }

  92.     }

  93.     public static List<File> getAsFileList(List<File> list, File f) throws Exception {
  94.         if (f.isDirectory()) {
  95.             String[] fileListTmp = f.list();
  96.             for (String str : fileListTmp) {
  97.                 Pattern pattern = Pattern.compile(".+\\.js$");
  98.                 Matcher m = pattern.matcher(str);
  99.                 if (m.matches()) {
  100.                     list.add(new File(f.getPath() + "\\" + str));
  101.                 } else {
  102.                     File tmp = new File(f.getPath() + "\\" + str);
  103.                     if (tmp.isDirectory() && !str.equals(".svn")) {
  104.                         getAsFileList(list, tmp);
  105.                     }
  106.                 }
  107.             }
  108.         } else {
  109.             if (f.getName().matches(".js")) {
  110.                 list.add(f);
  111.             }
  112.         }
  113.         // System.out.println("List size: " + list.size());

  114.         return list;
  115.     }
  116. }


阅读(1941) | 评论(0) | 转发(0) |
0

上一篇:Erlang简介

下一篇:iptables简单的配置文件

给主人留下些什么吧!~~