java在文本处理中的相关辅助工具类
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在文本处理中的相关辅助工具类的更多相关文章
- 获取Spring容器中Bean实例的工具类(Java泛型方法实现)
在使用Spring做IoC容器的时候,有的类不方便直接注入bean,需要手动获得一个类型的bean. 因此,实现一个获得bean实例的工具类,就很有必要. 以前,写了一个根据bean的名称和类型获取b ...
- Java.util.Math类--数学相关的工具类
Math类--数学相关的工具类 java.util.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作. public static double abs(double ...
- java中重要的多线程工具类
前言 之前学多线程的时候没有学习线程的同步工具类(辅助类).ps:当时觉得暂时用不上,认为是挺高深的知识点就没去管了.. 在前几天,朋友发了一篇比较好的Semaphore文章过来,然后在浏览博客的时候 ...
- Java匹马行天下之JavaSE核心技术——工具类
Java匹马行天之JavaSE核心技术——工具类 一.Object类 java.lang.ObjectObject类是所有类直接或间接的父类 常用的方法: toString():以字符串形式返回对象的 ...
- 类型转换辅助工具类TypeCaseHelper
package org.sakaiproject.util; import java.math.BigDecimal; import java.sql.Date; import java.sql.Ti ...
- Java并发(十五):并发工具类——信号量Semaphore
先做总结: 1.Semaphore是什么? Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源. 把它比作是控制流量的红绿灯,比如XX马路要 ...
- Java并发(十四):并发工具类——CountDownLatch
先做总结: 1.CountDownLatch 是什么? CountDownLatch 允许一个或多个线程等待其他线程(不一定是线程,某个操作)完成之后再执行. CountDownLatch的构造函数接 ...
- Java并发编程系列-(2) 线程的并发工具类
2.线程的并发工具类 2.1 Fork-Join JDK 7中引入了fork-join框架,专门来解决计算密集型的任务.可以将一个大任务,拆分成若干个小任务,如下图所示: Fork-Join框架利用了 ...
- URL相关的工具类
package com.opslab.util.web; import com.opslab.util.CharUtil;import com.opslab.util.CharsetUtil;impo ...
随机推荐
- ssh远程登录过程中卡住
1.首先排查网络连通性,查看网络是否通畅,远程端口是否开放 2.查看服务器复制,cpu,内存负载是否过大 3.检查ssh配置,查看以下配置是否这样配置 UseDNS no GSSAPIAuthenti ...
- 关于golang select的用法
1 go的信道 1.1 什么是信道 信道可以理解为go协程之间进行通信的通道. 1.2 信道的声明 所有的信道都关联一个类型,一旦关联了类型,该信道就只能传输该类型的数据,传输其它类型的数据的话就是非 ...
- urllib库认证,代理,cookie
认证,代理,cookie 1from urllib.request import HTTPBasicAuthHandler, HTTPPasswordMgrWithDefaultRealm, buil ...
- C语言数组名取地址。。。
int main(){ int a[5] = { 1, 2, 3, 4, 5 }; printf("%08X ,%08X ,%08X ,%08X", a, &a, a + ...
- C语言--一维数组,字符数组
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zuoyou1314/article/details/30799519 watermark/2/tex ...
- 说说无线路由器后门的那些事儿(1)-D-Link篇
[原创]说说无线路由器后门的那些事儿(1)-D-Link篇 作 者: gamehacker 时 间: 2013-11-29,11:29:19 链 接: http://bbs.pediy.com/sho ...
- Math、Date内置对象方法整理
Math : 内置的对象(构造函数)静态属性或静态方法. 一. Math.PI : 圆周率 ...
- 使用pdfjs插件在线预览PDF文件
前言 本文介绍在html中使用 pdfjs插件在线预览PDF文件的方法. 实现步骤 下载 pdfjs 并引入项目中 到PDFJS官网 http://mozilla.github.io/pdf.js/g ...
- CPU与GPU,我们应该使用哪个?
CPU与GPU,我们应该使用哪个? CPU与GPU CPU即中央处理器,GPU即图形处理器. 两者的相同之处:两者都有总线和外界联系,有自己的缓存体系,以及数字和逻辑运算单元 两者的区别之处:在于存在 ...
- AF封装的关于一次请求上传多图到服务器!!!
方式一:图片封装在模型数组中 /** * 上传多图到服务器 * * @param URLString 请求地址 * @param parameters 请求的其他参数 * ...