user846290
user846290

Reputation: 147

Parsing text / Arrange text in log file

I am trying to read a log file written in this form using java :

2 05.08.2011 13:07:54.136 .r5 objects - ODBC [128] - C:\Windows\example.r5 [30] - 0x081BF015 - TLineThread.ModuleODBC the ODBC driver reports an error - parameter strSQLError contains the error information 
1 09.08.2011 19:01:28.473 .r5 objects - ODBC [27] - C:\Windows\ExampleWithALongName.r5 [18] - 0x081BF015 - ODBC driver reports an error - parameter strSQLError contains the error information 

And what the wirte it in an output like this:

2 05.08.2011 13:07:54.136 .r5 objects - ODBC [128] - C:\Windows\example.r5 [30]              - 0x081BF015 - TLineThread.ModuleODBC the ODBC driver reports an error - parameter strSQLError contains the error information 
1 09.08.2011 19:01:28.473 .r5 objects - ODBC [27]  - C:\Windows\ExampleWithALongName.r5 [18] - 0x081BF015 - ODBC driver reports an error                            - parameter strSQLError contains the error information 

So that in every line, the delimiter "-" is exactly under the delimter "-" of the line before. (No mather if names of the path ar longer or different)

How should I do this?

Upvotes: 2

Views: 269

Answers (1)

shift66
shift66

Reputation: 11958

there are 6 parts and you should find the longest of each of them in lines.

    File file = new File(inputFilePath);
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        int[] partCharCount = new int[6];
        for(int c : partsCount)
            c=0;

        while((line=br.readLine())!=null)
        {
            String[] parts = line.split("\\s*-\\s*");
            for(int i=0;i<parts.length; i++)
                if(parts[i].length() > partCharCount[i])
                    partCharCount[i] = parts.length;
        }

after this you should read that file again, split each line to parts like in this code and then append to each part spaces to reach partsCount[i]+1

Upvotes: 1

Related Questions