我正在设法过滤我的文字档案。 我有众多名字,其中载有许多文本档案,文本档案中没有几个工作人员,每个工作人员都有10个分组/小组(我在这里只看到3个)。 但是,每一组/组别可能包含几种原始内容(我在这里展示了1和2)。
这是我关于大坝的文件,第1页。
# 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()) != "
"){ // 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
}
我的产物与第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 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