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