我正在寻找一种过滤我的文本文件的方法。我有很多文件夹名称包含许多文本文件,文本文件有几个没有工作人员,每个工作人员有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
答案 0 :(得分:1)
您的计数器会计算打印的行数,并且可以,如果您想计算有多少行,请将其移到if块之外:
while (( line = input.readLine()) != null){
if (!(line.startsWith("#") || line.startsWith("*") ||line.isEmpty() )) {
contents.append(line);
contents.append(separator);
System.out.println(line);
}
count++;
}
答案 1 :(得分:0)
一些建议:
这允许你编写一个这样的过滤器:
void parse() {
while( hasMoreLines() ) {
if( lineMatches() ) {
processLine();
}
}
}
boolean lineMatches() {
if( line.startsWith( "#" ) ) return false;
if( line.startsWith( "*" ) ) return false;
return true;
}
您现在有两个选择:您可以在1 1 1 1 1 1
或lineMatches()
中的某处检查模式processLine()
。
如果你采用后一种方法,你可以在写任何输出之前简单地return
。