1,java分词

package com.bobo.util;

import ICTCLAS.I3S.AC.ICTCLAS50;

public class Cutwords {
public static String Segment(String microblog) {
String textSeg = "";
try {
ICTCLAS50 testICTCLAS50 = new ICTCLAS50();
String argu = ".";
testICTCLAS50.ICTCLAS_Init(argu.getBytes("GB2312")); String sInput = microblog; byte nativeBytes[] = testICTCLAS50.ICTCLAS_ParagraphProcess(
sInput.getBytes("GB2312"), 0, 0);
String nativeStr = new String(nativeBytes, 0, nativeBytes.length,
"GB2312"); textSeg = nativeStr; } catch (Exception ex) { }
return textSeg;
}
}

CutWords

2,java文件读写

package com.bobo.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List; public class FileUtil { public static ArrayList<String> FileList = new ArrayList<String>(); /**
* 列出某個目錄及其子目錄下所有的文件列表
*
* @param filepath
* 目錄路徑
* @return 該路徑及其子路經下的所有文件名列表
* @throws FileNotFoundException
* @throws IOException
*/
public static List<String> readDirs(String filepath)
throws FileNotFoundException, IOException {
try {
File file = new File(filepath);
if (!file.isDirectory()) {
System.out.println("输入的不是目錄名称;");
System.out.println("filepath:" + file.getAbsolutePath());
} else {
String[] flist = file.list();
for (int i = 0; i < flist.length; i++) {
File newfile = new File(filepath + "/" + flist[i]);
if (!newfile.isDirectory()) {
FileList.add(newfile.getAbsolutePath());
} else if (newfile.isDirectory()) {
readDirs(filepath + "/" + flist[i]);
}
}
}
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
return FileList;
} /**
* 讀取文件內容,以字符串的方式返回
*
* @param file
* 需要讀取的文件名
* @return 返回讀取的文件內容構成的字符串,行之間用\r\n進行分割
* @throws FileNotFoundException
* @throws IOException
*/
public static String readFile(String file) throws FileNotFoundException,
IOException {
StringBuffer strSb = new StringBuffer(); // String is constant,
// StringBuffer can be
// changed.
InputStreamReader inStrR = new InputStreamReader(new FileInputStream(
file), "gbk"); // byte streams to character streams
BufferedReader br = new BufferedReader(inStrR);
String line = br.readLine();
while (line != null) {
strSb.append(line).append("\r\n");
line = br.readLine();
} return strSb.toString();
}
// 其他,一般读取文件的时候,利用bufferedReader方便,逐行写入文件的时候利用printStream比较方便 }

FileUtil

3,字符串工具类

package com.bobo.util;

import java.util.Stack;
import java.util.regex.Pattern; public class StringUtil {
/**
* 查找左右匹配型符号的位置
*
* @param str
* 需要查找的字符串
* @param cLeft
* 左侧符号
* @param cRight
* 右侧符号
* @return 返回和第一个左侧符号匹配的右侧符号位置,否则返回-1
*/ public static int findRightMatchChar(String str, String cLeft, String cRight) {
Stack<Integer> stack = new Stack<Integer>();
boolean pushAtLeastOnce = false;
for (int i = 0; i < str.length(); i++) { if (str.substring(i, i + 1).equals(cLeft)) {
stack.push(i);
pushAtLeastOnce = true;
}
if (str.substring(i, i + 1).equals(cRight)) {
stack.pop();
} if (pushAtLeastOnce && stack.isEmpty()) {
return i;
}
}
return -1;
} /**
* 判断是否为null或空�?
*
* @param str
* String
* @return true or false
*/
public static boolean isNullOrEmpty(String str) {
return str == null || str.trim().length() == 0;
} /**
* 判断str1和str2是否相同
*
* @param str1
* str1
* @param str2
* str2
* @return true or false
*/
public static boolean equals(String str1, String str2) {
return str1 == str2 || str1 != null && str1.equals(str2);
} /**
* 判断str1和str2是否相同(不区分大小写)
*
* @param str1
* str1
* @param str2
* str2
* @return true or false
*/
public static boolean equalsIgnoreCase(String str1, String str2) {
return str1 != null && str1.equalsIgnoreCase(str2);
} /**
* 判断字符串str1是否包含字符串str2
*
* @param str1
* 源字符串
* @param str2
* 指定字符�?
* @return true源字符串包含指定字符串,false源字符串不包含指定字符串
*/
public static boolean contains(String str1, String str2) {
return str1 != null && str1.contains(str2);
} /**
* 判断字符串是否为空,为空则返回一个空值,不为空则返回原字符串
*
* @param str
* 待判断字符串
* @return 判断后的字符�?
*/
public static String getString(String str) {
return str == null ? "" : str;
}
/**
* 判断字符串是否为数字
* @param str
* @return
*/
public static boolean isNumeric(Object str) {
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str.toString()).matches();
}
/**
* 判断字符串是否为英文字母
* @param str
* @return
*/
public static boolean isEnglish(Object str) {
Pattern pattern = Pattern.compile("[a-z]*");
return pattern.matcher(str.toString()).matches(); }
}

StringUtil

4,在java中运行shell命令的相关工具类

package com.bobo.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; public class CommandHelper {
// default time out, in millseconds
public static int DEFAULT_TIMEOUT;
public static final int DEFAULT_INTERVAL = 1000;
public static long START; public static void main(String[] args) {
DEFAULT_TIMEOUT = 10000;
try {
System.out
.println(new CommandHelper().exec("wc -l *.*").toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static CommandResult exec(String command) throws IOException,
InterruptedException {
Process process = Runtime.getRuntime().exec(command);
CommandResult commandResult = wait(process);
if (process != null) {
process.destroy();
}
return commandResult;
} private static boolean isOverTime() {
return System.currentTimeMillis() - START >= DEFAULT_TIMEOUT;
} private static CommandResult wait(Process process)
throws InterruptedException, IOException {
BufferedReader errorStreamReader = null;
BufferedReader inputStreamReader = null;
try {
errorStreamReader = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
inputStreamReader = new BufferedReader(new InputStreamReader(
process.getInputStream())); // timeout control
START = System.currentTimeMillis();
boolean isFinished = false; for (;;) {
if (isOverTime()) {
CommandResult result = new CommandResult();
result.setExitValue(CommandResult.EXIT_VALUE_TIMEOUT);
result.setOutput("Command process timeout");
return result;
} if (isFinished) {
CommandResult result = new CommandResult();
result.setExitValue(process.waitFor()); // parse error info
if (errorStreamReader.ready()) {
StringBuilder buffer = new StringBuilder();
String line;
while ((line = errorStreamReader.readLine()) != null) {
buffer.append(line);
}
result.setError(buffer.toString());
} // parse info
if (inputStreamReader.ready()) {
StringBuilder buffer = new StringBuilder();
String line;
while ((line = inputStreamReader.readLine()) != null) {
buffer.append(line);
}
result.setOutput(buffer.toString());
}
return result;
} try {
isFinished = true;
process.exitValue();
} catch (IllegalThreadStateException e) {
// process hasn't finished yet
isFinished = false;
Thread.sleep(DEFAULT_INTERVAL);
}
} } finally {
if (errorStreamReader != null) {
try {
errorStreamReader.close();
} catch (IOException e) {
}
} if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (IOException e) {
}
}
}
}
}

CommandHelper

package com.bobo.util;

public class CommandResult {
public static final int EXIT_VALUE_TIMEOUT = -1; private String output; void setOutput(String error) {
output = error;
} public String getOutput() {
return output;
} int exitValue; void setExitValue(int value) {
exitValue = value;
} int getExitValue() {
return exitValue;
} private String error; /**
* @return the error
*/
public String getError() {
return error;
} /**
* @param error
* the error to set
*/
public void setError(String error) {
this.error = error;
} @Override
public String toString() { return "output:" + this.output + ";error:" + this.error + ";exitValue:"
+ this.exitValue;
}
}

CommandResult

5,过滤某个目录下以特定后缀结尾的文件

package com.bobo.myinterface;

import java.io.File;
import java.io.FileFilter; public class MyFileFilter implements FileFilter {
private String suffix; public MyFileFilter(String suffix) {
this.suffix = suffix;
} @Override
public boolean accept(File arg0) {
if (arg0.isDirectory() || arg0.getAbsolutePath().endsWith(this.suffix)) {
return true;
} else {
return false;
}
} }

文件过滤器

在fileUtil中添加showAllFile方法

    public static void showAllFiles(File dir,FileFilter filter,ArrayList<File> fileList) {
File[] fs = dir.listFiles(filter);
for (int i = ; i < fs.length; i++) {
if (fs[i].isDirectory()) {
showAllFiles(fs[i],filter,fileList);
}else{
System.out.println(fs[i].getAbsolutePath());
fileList.add(fs[i]);
}
} }

showAllFile方法

最终调用

    File dataDir = new File(Constants.DataDir);
// 得到所有标注过的数据
ArrayList<File> fileList = new ArrayList<File>();
FileUtil.showAllFiles(dataDir, new MyFileFilter(".dealed"), fileList);
System.out.println(fileList.size());

列举特定后缀文件的调用方法

java在文本处理中的相关辅助工具类的更多相关文章

  1. 获取Spring容器中Bean实例的工具类(Java泛型方法实现)

    在使用Spring做IoC容器的时候,有的类不方便直接注入bean,需要手动获得一个类型的bean. 因此,实现一个获得bean实例的工具类,就很有必要. 以前,写了一个根据bean的名称和类型获取b ...

  2. Java.util.Math类--数学相关的工具类

    Math类--数学相关的工具类 java.util.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作. public static double abs(double ...

  3. java中重要的多线程工具类

    前言 之前学多线程的时候没有学习线程的同步工具类(辅助类).ps:当时觉得暂时用不上,认为是挺高深的知识点就没去管了.. 在前几天,朋友发了一篇比较好的Semaphore文章过来,然后在浏览博客的时候 ...

  4. Java匹马行天下之JavaSE核心技术——工具类

    Java匹马行天之JavaSE核心技术——工具类 一.Object类 java.lang.ObjectObject类是所有类直接或间接的父类 常用的方法: toString():以字符串形式返回对象的 ...

  5. 类型转换辅助工具类TypeCaseHelper

    package org.sakaiproject.util; import java.math.BigDecimal; import java.sql.Date; import java.sql.Ti ...

  6. Java并发(十五):并发工具类——信号量Semaphore

    先做总结: 1.Semaphore是什么? Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源. 把它比作是控制流量的红绿灯,比如XX马路要 ...

  7. Java并发(十四):并发工具类——CountDownLatch

    先做总结: 1.CountDownLatch 是什么? CountDownLatch 允许一个或多个线程等待其他线程(不一定是线程,某个操作)完成之后再执行. CountDownLatch的构造函数接 ...

  8. Java并发编程系列-(2) 线程的并发工具类

    2.线程的并发工具类 2.1 Fork-Join JDK 7中引入了fork-join框架,专门来解决计算密集型的任务.可以将一个大任务,拆分成若干个小任务,如下图所示: Fork-Join框架利用了 ...

  9. URL相关的工具类

    package com.opslab.util.web; import com.opslab.util.CharUtil;import com.opslab.util.CharsetUtil;impo ...

随机推荐

  1. 第八周课程总结&实验报告六

    实验六 Java异常 实验目的 理解异常的基本概念: 掌握异常处理方法及熟悉常见异常的捕获方法. 实验要求 练习捕获异常.声明异常.抛出异常的方法.熟悉try和catch子句的使用. 掌握自定义异常类 ...

  2. Jmeter响应数据显示乱码问题

    Jmeter在访问接口的时候,响应内容如果有中文可能会显示乱码,原因应该是响应页面没有做编码处理,jmeter默认按照ISO-8859-1编码格式进行解析. 解决步骤: 现象:jmeter访问本地文件 ...

  3. openmvg中cmd模块解析

    ---恢复内容开始--- 在openmvg库中,定义了一个CmdLine类来定义例程的输入参数格式.源文件为.\openMVG\src\third_party\cmdLine\cmdLine.h. 先 ...

  4. Solr安装(单机版)

    本文记录的是solr在win下安装配置使用的过程,最后将solr部署到Linux上通过远程访问.下一篇文章会介绍   solr集群搭建(SolrCloud)    的安装! Solr是基于Lucene ...

  5. Oracle 查询 in条件个数大于1000的解决方案

    Oracle 查询 in条件个数大于1000的解决方案,我所了解的有如下四种: 1. 把in分组再or: 思路:如果list的长度为2000,可以500个分一组,就有4个组,这4个组之间再or即可. ...

  6. MySql+EF+CodeFirst

    ef+mssql详细是许多.net程序员的标配.作为一个程序员当然不能只会mssql这一个数据库,今天简单聊聊ef+mysql.推荐新人阅读. 1]首先创建一个mvc项目,如图: 创建完毕之后再nug ...

  7. java中使用SimpleDateFormat实现字符串和日期的相互转换

    java中使用SimpleDateFormat实现字符串和日期的相互转换 import java.text.ParseException; import java.text.SimpleDateFor ...

  8. 猿题库 iOS 客户端架构设计(原文地址:http://gracelancy.com/blog/2016/01/06/ape-ios-arch-design/)

    猿题库 iOS 客户端架构设计 序 猿题库是一个拥有数千万用户的创业公司,从2013年题库项目起步到2015年,团队保持了极高的生产效率,使我们的产品完成了五个大版本和数十个小版本的高速迭代.在如此快 ...

  9. Fokker–Planck equation

    Fokker–Planck equation:https://en.wikipedia.org/wiki/Fokker%E2%80%93Planck_equation 随机微分方程:https://e ...

  10. 强化学习(Reinfment Learning) 简介

    本文内容来自以下两个链接: https://morvanzhou.github.io/tutorials/machine-learning/reinforcement-learning/ https: ...