public void readasbytes(){
FileExtractor cuter = new FileExtractor();
byte[] bytes = cuter.from("D:\11.txt").start(0).end(1048).readAsBytes();
}
public File splitAsFile(){
FileExtractor cuter = new FileExtractor();
return cuter.from("D:\11.txt").to("D:\22.txt").start(0).end(1048).extractAsFile();
}
public File appendText(){
FileExtractor cuter = new FileExtractor();
return cuter.from("D:\11.txt").to("D:\44.xml").appendAsFile("<Document><Body>", "</Body></Document>");
}
public String extractText(){
FileExtractor cuter = new FileExtractor();
return cuter.from("D:\11.txt").extractAsString(new EasyProcesser() {
@Override
public String finalStep(String line, int lineNumber, Status status) {
if(lineNumber==3){
status.shouldContinue = false;//表示不再继续读取文件内容
return line+"我好帅!";
}
return line.replaceAll("帅","");
}
});
}
public File killBugs(){
FileExtractor cuter = new FileExtractor();
return cuter.from("D:\bugs.txt").to("D:\nobug.txt").extractAsFile(new EasyProcesser() {
@Override
public String finalStep(String line, int lineNumber, Status status) {
return line.replaceAll("bug", "");
}
});
}
public interface Process{
/**
* @param b 本次读取的数据
* @param length 本次读取的有效长度
* @param currentIndex 当前读取到的位置
* @param available 读取文件的总长度
* @return true 表示继续读取文件,false表示终止读取文件
* @time 2017年1月22日 下午4:56:41
*/
public boolean doWhat(byte[] b,int length,int currentIndex,int available);
/**
*
* @param line 本次读取到的行
* @param currentIndex 行号
* @return true 表示继续读取文件,false表示终止读取文件
* @time 2017年1月22日 下午4:59:03
*/
public boolean doWhat(String line,int currentIndex);
public class IteratorFile implements Process
{
......
/**
* 按照字节来读取遍历文件内容,根据自定义需要重写该方法
*/
@Override
public boolean doWhat(byte[] b, int length,int currentIndex,int available) {
return true;
}
/**
* 按照行来读取遍历文件内容,根据自定义需要重写该方法
*/
@Override
public boolean doWhat(String line,int currentIndex) {
return true;
}
......
}
public void iterator2Bytes(){
init();
int length = -1;
FileInputStream fis = null;
try {
file = new File(in);
fis = new FileInputStream(file);
available = fis.available();
fis.skip(getStart());
readedIndex = getStart();
if (!beforeItrator()) return;
while ((length=fis.read(bytes))!=-1) {
readedIndex+=length;
if(!doWhat(bytes, length,readedIndex,available)){
break;
}
}
if(!afterItrator()) return;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void iterator2Line(){
init();
BufferedReader reader = null;
FileReader read = null;
String line = null;
try {
file = new File(in);
read = new FileReader(file);
reader = new BufferedReader(read);
if (!beforeItrator()) return;
while ( null != (line=reader.readLine())) {
readedIndex++;
if(!doWhat(line,readedIndex)){
break;
}
}
if(!afterItrator()) return ;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
read.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public IteratorFile from(String in){
this.in = in;
return this;
}
/**
* 往文件头或者文件结尾插入字符串
* @tips 不能对同一个文件输出路径反复执行该方法,否则会出现文本异常,因为用到了RandomAccessFile,如有需要,调用前需手动删除原有的同名文件
* @param startStr 文件开头要插入的字符串
* @param endStr 文件结尾要插入的字符串
* @return 生成的新文件
* @time 2017年1月22日 下午5:05:35
*/
public File appendAsFile(final String startStr,String endStr){}
/**
* 从指定位置截取文件
* @tips 适合所有的文件类型
* @return
* @time 2017年1月22日 下午5:06:36
*/
public File splitAsFile(){}
/**
* 文本文件的特殊处理(情景:文本抽取,文本替换等)
* @tips 只适合文本文件,对于二进制文件,因为换行符的原因导致文件出现可能无法执行等问题。
* @time 2017年1月22日 下午5:09:14
*/
public File extractAsFile(FlowLineProcesser method) {
/**
* 文本文件的特殊处理(情景:文本抽取,文本替换等)
* @tips 只适合文本文件,对于二进制文件,因为换行符的原因导致文件出现可能无法执行等问题。
* @time 2017年1月22日 下午5:09:14
*/
public String extractAsString(FlowLineProcesser method) {}
/**
* 读取指定位置的文件内容为字节数组
* @return
* @time 2017年1月23日 上午11:06:18
*/
public byte[] readAsBytes(){}
/**
* 设置源文件
*/
public FileExtractor from(String in){
this.in = in;
return this;
}
/**
* 设置生成临时文件的位置(返回值为File的方法均需要设置)
*/
public FileExtractor to(String out) {
this.out = out;
return this;
}
/**
* 文本开始截取的位置(包含此位置),字节相关的方法均需要设置
*/
public FileExtractor start(int start){
this.startPos = start;
return this;
}
/**
* 文本截取的终止位置(包含此位置),字节相关方法均需要设置
*/
public FileExtractor end(int end) {
this.endPos = end;
return this;
}
//本方法在需要读取的数据多时,不建议使用,因为byte[]是不可变的,多次读取的时候,需要进行多次的byete[] copy过程,效率“感人”。
public byte[] readAsBytes(){
try {
checkIn();
} catch (Exception e) {
e.printStackTrace();
return null;
}
//临时保存字节的容器
final BytesBuffer buffer = new BytesBuffer();
IteratorFile c = new IteratorFile(){
@Override
public boolean doWhat(byte[] b, int length, int currentIndex,
int available) {
if(readedIndex>endPos){
//说明已经读取到了endingPos位置并且读超了
buffer.addBytes(b, 0, length-(readedIndex-endPos-1)-1);
return false;
}else{
buffer.addBytes(b, 0, length-1);
}
return true;
}
};
//按照字节进行遍历
c.from(in).start(startPos).iterator2Bytes();
return buffer.toBytes();
}
public File splitAsFile(){
......
final OutputStream os = FileUtils.openOut(file);
try {
IteratorFile itFile = new IteratorFile(){
@Override
public boolean doWhat(byte[] b, int length,int readedIndex,int available) {
try {
if(readedIndex>endPos){
//说明已经读取到了endingPos位置,并且读超了readedIndex-getEnd()-1位
os.write(b, 0, length-(readedIndex-endPos-1));
return false;//终止读取
}else{
os.write(b, 0, length);
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}.from(in).start(startPos);
itFile.iterator2Bytes();
} catch (Exception e) {
e.printStackTrace();
this.tempFile = null;
}finally{
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return getTempFile();
}
public class Status{
/**
* 是否找到了开头,默认false,若true则后续的遍历不会执行相应的firstStep()方法
*/
public boolean overFirstStep = false;
/**
* 是否找到了结尾,默认false,若true则后续的遍历不会执行相应的finalStep()方法
*/
public boolean overFinalStep = false;
/**
* 是否继续读取源文件,默认true表示继续读取,false则表示,执行本次操作后,遍历终止
*/
public boolean shouldContinue = true;
}
public interface FlowLineProcesser{
/**
*
* @param line 读取到的行
* @param lineNumber 行号,从1开始
* @param status 控制器
* @return
* @time 2017年1月22日 下午5:02:02
*/
String firstStep(String line,int lineNumber,Status status);
/**
* @tips
* @param line 读取到的行(是firstStep()处理后的结果)
* @param lineNumber 行号,从1开始
* @param status 控制器
* @return
* @time 2017年1月22日 下午5:02:09
*/
String finalStep(String line,int lineNumber,Status status);
}
public String extractAsString(FlowLineProcesser method) {
try {
checkIn();
} catch (Exception e) {
e.printStackTrace();
return null;
}
final StringBuilder builder = new StringBuilder();
this.mMethod = method;
new IteratorFile(){
Status status = new Status();
@Override
public boolean doWhat(String line, int currentIndex) {
String lineAfterProcess = "";
if(!status.overFirstStep){
lineAfterProcess = mMethod.firstStep(line, currentIndex,status);
}
if(!status.shouldContinue){
return false;
}
if(!status.overFinalStep){
lineAfterProcess = mMethod.finalStep(lineAfterProcess,currentIndex,status);
}
if(lineAfterProcess!=null){
builder.append(lineAfterProcess);
builder.append(getLineStr());//换行符被写死在这里了
}
if(!status.shouldContinue){
return false;
}
return true;
}
}.from(in).iterator2Line();
return builder.toString();
}
public File extractAsFile(FlowLineProcesser method) {
try {
checkIn();
checkOut();
} catch (Exception e) {
e.printStackTrace();
return null;
}
this.mMethod = method;
File file = initOutFile();
if(file==null){
return null;
}
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(file);
} catch (Exception e) {
e.printStackTrace();
return null;
}
final BufferedWriter writer = new BufferedWriter(fileWriter);
IteratorFile itfile = new IteratorFile(){
Status status = new Status();
@Override
public boolean doWhat(String line, int currentIndex) {
String lineAfterProcess = "";
if(!status.overFirstStep){
lineAfterProcess = mMethod.firstStep(line, currentIndex,status);
}
if(!status.shouldContinue){
return false;
}
if(!status.overFinalStep){
lineAfterProcess = mMethod.finalStep(lineAfterProcess,currentIndex,status);
}
if(lineAfterProcess!=null){
try {
writer.write(lineAfterProcess);
writer.newLine();//TODO 换行符在此给写死了
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
if(!status.shouldContinue){
return false;
}
return true;
}
};
itfile.from(in).iterator2Line();
if(writer!=null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
return getTempFile();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有