今天发先从JavaEye网站上粘贴下来的代码有一些前面会带有编号,在程序中无法使用,如果手动将其编号删除实在是太浪费时间了,所以自己谢了一个简单的程序用来自动去除编号。
1.只要将有编号的代码保存到一个文件中。
2.在程序中指定文件的路径。
3.直接运行就可以将转换好的文件自动保存到原来的文件中(也可以自从新指定路径)
主意:两个文件不在同一个包下,用时需要更改
package com.cui.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileIO {
private String filePath;
private File file = null;
private FileReader fr = null;
private FileWriter fw = null;
private BufferedReader br = null;
private BufferedWriter bw = null;
public FileIO() {};
/**
* 得到指定文件路径
* @param filePath 指定文件路径
*/
public FileIO(String filePath) {
this.filePath = filePath;
}
/**
* 读取指定文件的内容并返回
* @return 指定文件字内容
* @throws IOException
*/
public String readFileToStr() {
StringBuffer sb = new StringBuffer();
if(file == null) file = new File(filePath);
try {
fr = new FileReader(file);
br = new BufferedReader(fr);
String str = "";
while(str != null) {
str = br.readLine();
if(str != null) sb.append("\n"+str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
close();
}
return sb.toString();
}
/**
* 读取指定文件的内容并返回
* @param filePath 指定文件路径
* @return 指定文件字内容
* @throws IOException
*/
public String readFileToStr(String filePath) {
this.filePath = filePath;
return readFileToStr();
}
/**
* 将指定字符串写入指定文件
* @param str 指定字符串
* @throws IOException
*/
public void writeStrToFile(String str) {
if(file == null) file = new File(filePath);
try {
fw = new FileWriter(file);
bw = new BufferedWriter(fw);
bw.write(str);
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
close();
}
}
/**
* 将指定字符串写入指定文件
* @param str 指定字符串
* @param filePath 指定文件路径
* @throws IOException
*/
public void writeStrToFile(String str,String filePath){
this.filePath = filePath;
writeStrToFile(str);
}
/**
* 将所有的i/o流关闭
*/
public void close() {
if(br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.cui.tool;
import com.cui.io.FileIO;
public class DeleteNumber {
public String delNum(String str) {
int len = str.length(); //得到字符的长度
char ch1 = ' '; //创建一个相同长度的字符数组
char ch2 = ' ';
StringBuffer sb = new StringBuffer();
for(int i=0;i<len-1;i++){
ch1 = str.charAt(i); //赋值
ch2 = str.charAt(i+1);
if(ch1 >=48&&ch1 <= 57 && ch2 >=48&&ch2 <= 57){
continue;
}if(ch1 >=48&&ch1 <= 57 && ch2 =='.') {
i += 1;
continue;
}else{
sb.append(ch1);
}
}
return sb.toString();
}
public static void main(String[] args) {
DeleteNumber dn = new DeleteNumber();
FileIO fio = new FileIO("D://a.txt");
String str = null;
str = fio.readFileToStr();
str = dn.delNum(str);
fio.writeStrToFile(str);
}
}
分享到:
相关推荐
NULL 博文链接:https://cqh520llr.iteye.com/blog/1978282
铸铁机前部安全技术规程.docx
在方法内的指定位置,分为两种:方法内的最前部和最后部。 三、 代码规范器使用方法 代码规范器的使用方法有两种: 命令行方式 格式:代码规范器 程序文件 调整操作分类 其中,调整操作分类 “+”表示正调整...
行业资料-交通装置-一种汽车前部通风装置.zip
关于用于乘用车的前部模块的说明.rar
15用于乘用车的前部模块_new.pdf
电动汽车安全性和车身前部吸能装置研究.pdf
行业分类-设备装置-具有前部装填式订书钉卡盒的订书机.zip
计算机三维辅助产品设计RHINO-投影仪前部建模.docx
电子政务-具有可重新配置的前部的电子设备.zip
行业资料-交通装置-一种汽车罩前部装置.zip
某乘用车前部悬架控制臂轻量化设计.pdf
125车辆的前部,玩具和或其复制品_new.pdf
行业资料-交通装置-一种新型的车身前部结构.zip
行业资料-交通装置-一种汽车用前部通风管.zip
关于车辆的前部,玩具和或其复制品的介绍说明.rar
更换前部溜子过渡槽和采煤机前滚筒方轴安全技术措施.docx
59用于通过机动车辆评估前部元件的特征的方法_new.pdf
关于用于通过机动车辆评估前部元件的特征的方法的介绍说明.rar
优化软件OPTIMUS案例—车辆前部结构优化设计(PAMCRASH、MADYMO)参考.pdf