English 中文(简体)
阅读文本文档,并写到若干文本档案中,以进行过滤/处理
原标题:Read a text file and write to several text file for filtering/extraction

我正在设法过滤我的文字档案。 我有众多名字,其中载有许多文本档案,文本档案中没有几个工作人员,每个工作人员都有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
问题回答

你的反省是印刷了多少条线的,如果你想计算有多少条线,则会将其移至哪一栏:

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

Some advice:

  1. Split long methods into several, smaller ones
  2. If that s hard because you use many local variables, move everything into a parser class and make each local variable a field.
  3. Each method should do one single thing.

这使你能够写上这样的过滤器:

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 1上查看<>条码>,<>条码/代码>或<条码>中某些地方。

如果你采取后一种做法,你可以在撰写任何产出之前简单地读到“条形”。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签