简体   繁体   中英

How can I read/get some specific informations from txt file using Java

I'm facing some difficulties to get some specific information from many *.log files using Java. I've spent some hours looking into this forum but I didn't found something like I need. First of all I wanna get those information and print them at the console, just to make sure all information that I need are working fine. The data from *.log file are this kind below:

Date Logged:        10-22-2014 11:11:00 AM
Task Name:      PROJECT_X_X00_SPI3
User ID:        Operator Mode
System:         PP/PS Series FlashCORE
Machine ID:     12345678910

Device:         MACRONIX MX25L6473EM2 SO8
Data Source:        X:\XPTO\PROJECT\BINARYFILE.bin
Sumcheck:       12345678
Process:        Continuity/Blank Check/ID Check/Erase/Program/Verify/Auto Secure/Mark/Vision
Process Status:     Job Begin Status: New Job, Job End Status: Stopped
TLWin Session ID:   < none >

Job Start Time:     10-22-2014 03:38:57 AM
Job End Time:       10-22-2014 11:11:00 AM
Devices Total:      5051
Devices Passed:     5041
Devices Failed:     10
Overall Device Yield:   99.80%

Task Description:   MX25L6473EM

Nominal Throughput: 722 dph
Job Throughput:     679 dph

Here's the code that I made in order to read all data from *.log files:

public class ReadingDataTxt {

public static void main(String[] args) {

    String dir = "F:\\XPTO";

    File file = new File(dir);

    for (String arq : file.list()) {
        if (arq.endsWith(".log")) {
            System.out.println("--------->" + arq + "<---------");
            try {
                System.out.println(reading(dir + "\\" + arq));
            } catch (Exception e) {

            }
        }
    }
}

private static String reading(String dir) throws Exception {
    String line = " ", content = " ";
    BufferedReader br = new BufferedReader(new FileReader(new File(dir)));
    while ((line = br.readLine()) != null) {
        if (!line.isEmpty()) {
            content = new StringBuilder(content).append(line.concat("\n")).toString();

        }
    }

    br.close();
    return content;
}

}

The most important information that I need is the data a front the first String (strong highlight): Date Logged: 10-22-2014 Task Name: PROJECT_X_X00_SPI3 Device: MACRONIX MX25L6473EM2 SO8 Devices Total: 5051 Devices Failed: 10 Overall Device Yield: 99.80%

The second step will be make a CSV file in order to place these information into the data base.

Thanks so much!

You can use Java version of Grok Parser. You can define a pattern and all the lines in the file that match the pattern will get parsed.

Providing a pseudo-code
Either you can use StringBuilder itself and the parsing logic given below to generate the required string. Just make sure you are defining StringBuilder outside of any loop.
OR

you can take a map Map<String, String> data = new HashMap<>(); and populate it by required key/value(s). So that you can extract it in some other method by keys.

now for each line, you can do

String[] tokens = line.trim().split(":");
if(tokens.length >= 2){
        data.put(tokens[0].trim(), tokens[1].trim());
        // or append to stringbuilder
}

Additionally you can also put up a check there for the specific tokens(keys) only which you want to put into map.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM