代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
文本中提取特定字符串的程序
作者:
DDT
/ 发布于
2013/1/15
/
827
import java.io.*; import java.util.*; class TextRep { // the main function public static void main (String[] args) { if(args.length!=2&&args.length!=3) { // invalid number of arguments specified at command line System.out.println("Usage(s):\n"); System.out.println(" 1. java TextRep file_to_process \"search_for_word\" \"replace_with_word\""); System.out.println(" 2. java TextRep file_to_process file_of_words_to_search_replace"); return; } try { // make 32k buffer for output StringBuffer strOutput = new StringBuffer(32768); // read input file into a byte array byte[] pInput = ReadFile(args[0]); // make a backup copy WriteFile(args[0]+".backup.copy",pInput); String strInput = new String(pInput); if(args.length==3) { // check if words are empty if(args[1].equals("")||args[2].equals("")) { System.out.println("Cannot process empty words"); return; } System.out.println("Replacing \""+args[1]+"\" with \""+args[2]+"\" in file: "+args[0]); // find all instances of args[1] and replace it with args[2] int nPos = 0; while(true) { int nIndex = strInput.indexOf(args[1],nPos); // if args[1] can no longer be found, then copy the rest of the input if(nIndex<0) { strOutput.append(strInput.substring(nPos)); break; } // otherwise, replace it with args[2] and continue else { strOutput.append(strInput.substring(nPos,nIndex)); strOutput.append(args[2]); nPos = nIndex+args[1].length(); } } strInput = strOutput.toString(); } else if(args.length==2) { System.out.println("Processing file: "+args[0]); // create a string tokenizer with file args[1] StringTokenizer tokens = new StringTokenizer(new String(ReadFile(args[1])),"\r\n"); // check to see of the tokenizer has even number of tokens int nCount = tokens.countTokens(); if(nCount<1||nCount%2!=0) { System.out.println("Invalid number of non-empty lines in file: "+args[1]); return; } // for each pair of string tokens, replace the first one with the next one nCount = nCount/2; for(int i=0;i<nCount;i++) { // string to search in the input String strSearch = tokens.nextToken(); // string used to replace the previous one String strReplace = tokens.nextToken(); // check if words are empty if(strSearch.equals("")||strReplace.equals("")) { System.out.println("Cannot process empty words"); return; } // replace each instance of strSearch with strReplace System.out.println("Replacing \""+strSearch+"\" with \""+strReplace+"\""); int nPos = 0; while(true) { int nIndex = strInput.indexOf(strSearch,nPos); // if strSearch can no longer be found, then copy the rest of the input if(nIndex<0) { strOutput.append(strInput.substring(nPos)); break; } // otherwise, replace it with strReplace and continue else { strOutput.append(strInput.substring(nPos,nIndex)); strOutput.append(strReplace); nPos = nIndex+strSearch.length(); } } // continue to process the next pair of string tokens strInput = strOutput.toString(); strOutput = new StringBuffer(32768); } } // write the output string to file WriteFile(args[0],strInput.getBytes()); } catch(Exception e) { System.out.println(e.getMessage()); } } // helper function to read a file into a byte array static public final byte[] ReadFile(String strFile) throws IOException { int nSize = 32768; // open the input file stream BufferedInputStream inStream = new BufferedInputStream(new FileInputStream(strFile),nSize); byte[] pBuffer = new byte[nSize]; int nPos = 0; // read bytes into a buffer nPos += inStream.read(pBuffer,nPos,nSize-nPos); // while the buffer is filled, double the buffer size and read more while(nPos==nSize) { byte[] pTemp = pBuffer; nSize *= 2; pBuffer = new byte[nSize]; System.arraycopy(pTemp,0,pBuffer,0,nPos); nPos += inStream.read(pBuffer,nPos,nSize-nPos); } // close the input stream inStream.close(); if(nPos==0) { return "".getBytes(); } // return data read into the buffer as a byte array byte[] pData = new byte[nPos]; System.arraycopy(pBuffer,0,pData,0,nPos); return pData; } // helper function to write a byte array into a file static public final void WriteFile(String strFile, byte[] pData) throws IOException { BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(strFile),32768); if(pData.length>0) outStream.write(pData,0,pData.length); outStream.close(); } }
试试其它关键字
特定字符串
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
实现测量程序运行时间及cpu使用时间
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
DDT
贡献的其它代码
(
160
)
.
Oracle统计表的数据行和数据块信息
.
html标签闭合检测与修复
.
Powershell日期计算
.
Powershell的Base64编解码
.
Powershell并行循环
.
Powershell目录中搜索文本
.
Powershell枚举远程机器上的本地权限组
.
VBScript解析csv文件
.
快速排序之Powershell
.
批处理输出格式化时间字符串
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3