gpt4 book ai didi

java - 读取一个文本文件并写入多个文本文件以进行过滤/提取

转载 作者:行者123 更新时间:2023-12-01 15:22:52 28 4
gpt4 key购买 nike

我正在寻找一种过滤文本文件的方法。我有许多文件夹名称,其中包含许多文本文件,文本文件有几个没有人员,每个人员有 10 个群集/组(我在这里只显示了 3 个)。但是每个组/簇可能包含几个原语(我在这里展示了 1 和 2)

这是我给 Adam 的文本文件,page1_d.txt:

 # staff No. 0 //no of staff

* 0 0 1 //no of cluster

1 1 1 1 1 1 //one primitive here actually p1 in my Array

* 0 1 1

1 1 1 1 1 1

* 0 2 1

1 1 1 1 1 1

* 0 3 1

1 1 1 1 1 1

# staff No. 1

* 1 0 2

2 2 2 2 2 2 2 // two primitive here actually p2 and p3 in my Array
3 3 3 3 3

* 1 1 2

2 2 2 2 2 2 2
3 3 3 3 3

* 1 2 2

2 2 2 2 2 2 2
3 3 3 3 3

* 1 3 2

2 2 2 2 2 2 2
3 3 3 3 3

我的公开课:

public class NewClass {
String [][][][] list = { {{{"adam"}},{{"p1"},{"p2","p3"}},{{"p4","p5","p6"}}} };
// first is name for the folder, second is num of text file, 3rd num of staff, 4th num of primtive
final int namefolder = list.length;
final int page = list[0].length;

这是我读取文本文件并写入另一个文本文件的方法:

public void search (File folder, int name,int numPage){
BufferedReader in;
String line=null;
int staff = list[name][numPage].length; // the length vary for each txt file

for (int numStaff=0;numStaff<staff;numStaff++){
int primitive = list[name][numPage][numStaff].length; //the length vary for each staff
StringBuilder contents = new StringBuilder();
String separator = System.getProperty("line.separator");

try {
in = new BufferedReader(new FileReader(folder));
for (int numPrim=0;numPrim<primitive;numPrim++) {
while(( line = in.readLine()) != null) {
if (!(line.startsWith("#") ||line.startsWith("*") ||line.isEmpty() )) {
contents.append(line);
contents.append(separator);
try {
PrintWriter output = new PrintWriter(new FileOutputStream("C://"+list[name][numPage][numStaff][numPrim]+".txt", true));
try {
output.println(line);
}
finally {
output.close();
}
for (int i = numPrim+1; i < primitive; i++){ // this is for more than 2 primitive
if((line = in.readLine()) != "\n"){ // 2nd or more primitive has no newline
contents.append(line);
contents.append(separator);
try {
PrintWriter output2 = new PrintWriter(new FileOutputStream("C://"+list[name][numPage][numStaff][i]+".txt", true));

try {
output2.println(line);
}
finally {
output2.close();
}
} catch (IOException e) {
System.out.println("Error cannot save");
e.printStackTrace();
}
}
}
} catch (IOException e) {
System.out.println("Error cannot save");
e.printStackTrace();
}
}
}//end of while
}// end for loop for prim
in.close();
}//end of try
catch(IOException e) {
e.printStackTrace();
}
}// end for loop for staff
}

我希望 p1.txt 的输出如下:

1 1 1 1 1 1 
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1

但它显示了这一点:

1 1 1 1 1 1 
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
(empty)
2 2 2 2 2 2 2
3 3 3 3 3
2 2 2 2 2 2 2
3 3 3 3 3
2 2 2 2 2 2 2
3 3 3 3 3
2 2 2 2 2 2 2
3 3 3 3 3

还有这个(p2.txt):

1 1 1 1 1 1 
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
2 2 2 2 2 2 2
2 2 2 2 2 2 2
2 2 2 2 2 2 2
2 2 2 2 2 2 2

还有这个(p3.txt):

(empty)    
(empty)
(empty)
(empty)
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3

最佳答案

你的计数器计算打印了多少行,这是可以的,如果你想计算有多少行,请将其移到 if block 之外:

while (( line = input.readLine()) != null){
if (!(line.startsWith("#") || line.startsWith("*") ||line.isEmpty() )) {
contents.append(line);
contents.append(separator);
System.out.println(line);
}
count++;
}

关于java - 读取一个文本文件并写入多个文本文件以进行过滤/提取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10619571/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com