求Java代码:将D盘a.txt文件中含有"AA","BB"的行留下,其余的都删除
答案:6 悬赏:10 手机版
解决时间 2021-03-28 18:04
- 提问者网友:留有余香
- 2021-03-28 14:51
求Java代码:将D盘a.txt文件中含有"AA","BB"的行留下,其余的都删除
最佳答案
- 五星知识达人网友:长青诗
- 2021-03-28 15:06
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
getDelete("D:\a.txt");
}
private static void getDelete(String pathName)throws Exception{
File file = new File(pathName);
if(!file.exists())
throw new RuntimeException("异常:文件不存在!");
BufferedReader br = new BufferedReader(new FileReader(file));
List list = new ArrayList();
String line = "";
while((line=br.readLine())!=null){
if((line.indexOf("AA")!=-1) || (line.indexOf("BB")!=-1))
list.add(line);
}
br.close();
getWriter(pathName,list);
}
private static void getWriter(String pathName,List list)throws Exception{
BufferedWriter bw = new BufferedWriter(new FileWriter(pathName));
for(int i = 0; i < list.size(); i++){
bw.write(list.get(i));
bw.newLine();
}
bw.close();
}
}//测试文件内容:
AAasdasdsad
akldjalfd
fdgjdlkjh
gfhljlh
sdjfsdfBBsdflskdf
BBsdfsdf
asdkjlkg
dfkgjdfklgjdfg
AA
//最后测试文件结果:
AAasdasdsad
sdjfsdfBBsdflskdf
BBsdfsdf
AA
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
getDelete("D:\a.txt");
}
private static void getDelete(String pathName)throws Exception{
File file = new File(pathName);
if(!file.exists())
throw new RuntimeException("异常:文件不存在!");
BufferedReader br = new BufferedReader(new FileReader(file));
List
String line = "";
while((line=br.readLine())!=null){
if((line.indexOf("AA")!=-1) || (line.indexOf("BB")!=-1))
list.add(line);
}
br.close();
getWriter(pathName,list);
}
private static void getWriter(String pathName,List
BufferedWriter bw = new BufferedWriter(new FileWriter(pathName));
for(int i = 0; i < list.size(); i++){
bw.write(list.get(i));
bw.newLine();
}
bw.close();
}
}//测试文件内容:
AAasdasdsad
akldjalfd
fdgjdlkjh
gfhljlh
sdjfsdfBBsdflskdf
BBsdfsdf
asdkjlkg
dfkgjdfklgjdfg
AA
//最后测试文件结果:
AAasdasdsad
sdjfsdfBBsdflskdf
BBsdfsdf
AA
全部回答
- 1楼网友:玩家
- 2021-03-28 19:46
看起来可以,你能上网,一切都能干了!!
- 2楼网友:逐風
- 2021-03-28 18:55
用文件输出流,每次读取一行,然后如果indexOf去判断,有的话就拼接字符串。再保存覆盖文件就好了!追问具体的代码怎么实现
- 3楼网友:怙棘
- 2021-03-28 18:08
文件里保存的是什么类型的数据
- 4楼网友:醉吻情书
- 2021-03-28 17:00
public static void main(String[] args) {
File file = new File("D:\a.txt");
try {
String fileText = FileUtils.readFileToString(file);
Scanner scanner = new Scanner(fileText);
List outList = new ArrayList();
while (scanner.hasNext()) {
String fileLine = scanner.nextLine();
if(fileLine.indexOf("AA")>=0 || fileLine.indexOf("BB")>=0){
outList.add(fileLine);
}
}
FileWriter fileWriter = new FileWriter(file);
fileWriter.write("");
for (String string : outList) {
fileWriter.write(string+" ");
}
fileWriter.close();
} catch (IOException e) {
}
}
FileUtils.readFileToString 在commons-io.jar
File file = new File("D:\a.txt");
try {
String fileText = FileUtils.readFileToString(file);
Scanner scanner = new Scanner(fileText);
List
while (scanner.hasNext()) {
String fileLine = scanner.nextLine();
if(fileLine.indexOf("AA")>=0 || fileLine.indexOf("BB")>=0){
outList.add(fileLine);
}
}
FileWriter fileWriter = new FileWriter(file);
fileWriter.write("");
for (String string : outList) {
fileWriter.write(string+" ");
}
fileWriter.close();
} catch (IOException e) {
}
}
FileUtils.readFileToString 在commons-io.jar
- 5楼网友:千杯敬自由
- 2021-03-28 16:24
两种办法:
1、把含AA、BB的行保存,等文件读写完之后重写写入
2、新进个文件把读取到的行写入,读完之后删除源文件追问具体的代码怎么实现
1、把含AA、BB的行保存,等文件读写完之后重写写入
2、新进个文件把读取到的行写入,读完之后删除源文件追问具体的代码怎么实现
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯