如何删除文件中某些特定的字符

Darling_sheep 2011-01-27 03:15:48
各位大侠,怎样删除文件中某些特定的字符?
我有一个文件(文件地址是:E:/Websites/搜索.txt)存放了很多网站的名字和URL,里面是这些内容:
*
Website Name:***
Website URL: ***
以这样的形式重复出现
现在我需要一个函数来删除某一个网站的信息(如:百度)
*
Website Name:百度
Website URL: www.baidu.com
该怎样实现?50分求助.........
...全文
380 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
gamefx 2011-01-28
  • 打赏
  • 举报
回复
用过滤功能
Darling_sheep 2011-01-28
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 ilrxx 的回复:]

你了解了中文分词中stopWord的过滤方式后,这个问题会变的很简单。
[/Quote]
能不能具体解释一下stopWord?谢谢!
ilrxx 2011-01-28
  • 打赏
  • 举报
回复
你了解了中文分词中stopWord的过滤方式后,这个问题会变的很简单。
Darling_sheep 2011-01-28
  • 打赏
  • 举报
回复
终于搞定了这个程序,现在把代码贴出来分享一下!
因为我要删除的是三行数据,比如:
*
Website Name:百度
Website URL: www.baidu.com
所以我把6楼阿宝同学的代码稍微改了一下,但不得不佩服阿宝同学是个高手——膜拜!

import java.io.*;
public class Remove {
public static void main(String[] args){
try {
File inFile = new File("E:/Websites/搜索.txt");
File outFile = new File("E:/Websites/临时.txt");
BufferedReader br = new BufferedReader(new FileReader(inFile));
BufferedWriter bw = new BufferedWriter(new FileWriter(outFile));
for (String line1=br.readLine(),line2=br.readLine(),line3=br.readLine();
line3 != null;
line1=br.readLine(),line2=br.readLine(),line3=br.readLine()) {
if (line2.indexOf("百度") > 0) {continue;} //这个indexOf就可以判断是否存在特殊字符
String line11=line1+'\n',line22=line2+'\n',line33=line3;
String seperator=System.getProperty("line.separator");
line11=line11.replace("\n", seperator);
line22=line22.replace("\n", seperator);//若不这样处理打印出的换行符就是一个黑点
bw.write(line11);
bw.write(line22);
bw.write(line33);
bw.newLine();
}
br.close();
bw.flush();
bw.close();
if (inFile.delete()) { //删除 搜索.txt
outFile.renameTo(inFile); //把 临时.txt改名为 搜索.txt
}
System.out.println("Done!!"); //检验以上程序是否可以执行
} catch (IOException e) {
System.out.println("Erro!!"); //出现错误就提示
}
}
}
whut_lcy 2011-01-28
  • 打赏
  • 举报
回复
String.replace()
纠结的程序猿 2011-01-28
  • 打赏
  • 举报
回复
直接用PilotEdit Lite进行多行文本替换就可以了
风影萧诺 2011-01-27
  • 打赏
  • 举报
回复
6楼的代码很好嘛!
Darling_sheep 2011-01-27
  • 打赏
  • 举报
回复
回复6楼:高,实在是高,写的太犀利了!谢谢啦,回头给你分数!!
liumingchang 2011-01-27
  • 打赏
  • 举报
回复
正则表达式
qybao 2011-01-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 darling_sheep 的回复:]
回复1-3楼:谢谢你们的建议,batch是怎么写的?写好了怎样用程序调用呢?另外,过滤应该使用哪个函数和参数?
[/Quote]
举个简单的例子(复杂的例子还可以让batch带参数,这样可以指定删除的特殊字符,这里就懒得写了)
batch
把以下代码复制粘贴到一个文本文件,把文本文件重名为为filter.bat
--------------------------------------
@echo off
cd E:\Websites
findstr /V 百度 搜索.txt > xx.txt
del 搜索.txt
ren xx.txt 搜索.txt
@echo on
---------------------------------------

以下是java调用
String cmd = "filter.bat"; //这里是batch在系统的路径,如果有路径就把路径带上,没有的话就是缺省的当前路径
Runtime.getRuntime().exec(cmd);


至于另一种方法通过文件读写,一条一条过滤,简单说一下吧
File inFile = new File("E:/Websites/搜索.txt");
File outFile = new File("E:/Websites/临时.txt");
BufferedReader br = new BufferedReader(new BufferedInputSream(new FileInputStream(inFile)));
BufferedWriter bw = new BufferedWriter(new BufferedOutputStream(new FileOutputStream(outFile)));
for (String line=br.readLine(); line != null; line=br.readLine()) {
if (line.indesOf("百度") > 0) {continue;} //这个indexOf就可以判断是否存在特殊字符
bw.write(line);
bw.newLine();
}
br.close();
bw.flush();
bw.close();
if (inFile.delete()) { //删除 搜索.txt
outFile.renameTo(inFile.getName()); //把 临时.txt改名为 搜索.txt
}
dracularking 2011-01-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qybao 的回复:]

写个batch
cd E:\Websites
findstr /V 百度 搜索.txt > xx.txt
del 搜索.txt
ren xx.txt 搜索.txt

java里通过Runtime.getRuntime().exec执行batch
[/Quote]
这个还是相当方便快捷的
Darling_sheep 2011-01-27
  • 打赏
  • 举报
回复
回复1-3楼:谢谢你们的建议,batch是怎么写的?写好了怎样用程序调用呢?另外,过滤应该使用哪个函数和参数?
txzsp 2011-01-27
  • 打赏
  • 举报
回复
可以先读入文件内容,然后用正则表达式进行过滤,最好再进行保存。
#1楼的朋友的确有创意,打开一个新的思路,使用命令行方式的确是个不错的方法,不过可能在跨平台型上略有不足。

希望能给LZ有帮助!
zn85600301 2011-01-27
  • 打赏
  • 举报
回复
读取文件 readline 将你要过滤的文字过滤掉 然后另存个文件
qybao 2011-01-27
  • 打赏
  • 举报
回复
写个batch
cd E:\Websites
findstr /V 百度 搜索.txt > xx.txt
del 搜索.txt
ren xx.txt 搜索.txt

java里通过Runtime.getRuntime().exec执行batch

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧