0

テキスト ファイルをフィルタリングする方法を探しています。多くのテキスト ファイルを含む多くのフォルダ名があり、テキスト ファイルにはいくつかのスタッフがなく、各スタッフには 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
4

2 に答える 2

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++;
}
于 2012-05-16T13:30:38.380 に答える
0

いくつかのアドバイス:

  1. 長いメソッドをいくつかの小さなメソッドに分割する
  2. 多くのローカル変数を使用するためにそれが難しい場合は、すべてをパーサー クラスに移動し、各ローカル変数をフィールドにします。
  3. 各メソッドは 1 つのことを行う必要があります。

これにより、次のようなフィルターを作成できます。

void parse() {
    while( hasMoreLines() ) {
        if( lineMatches() ) {
            processLine();
        }
    }
}

boolean lineMatches() {
    if( line.startsWith( "#" ) ) return false;
    if( line.startsWith( "*" ) ) return false;

    return true;
}

次の 2 つのオプションがあります。または のどこかでパターン1 1 1 1 1 1を確認できます。lineMatches()processLine()

後者のアプローチを取る場合は、return出力を書き込む前に簡単に行うことができます。

于 2012-05-18T08:40:10.970 に答える