简体   繁体   中英

Java Buffer reader data from text file with comma manipulation

I'm new to Java programming and I'm challenging myself to do this one but it seems when it comes to buffer reader and writer I get lost. I have two text file that I parsing out from csv file, and I want to compare the data between this textfile.

This is what looks like inside the text file.

file1.text
sample1,true,online


file2.text
sample1,true,test,check,OL

expected output :
valid

file3.text
online,OL
offline,OF
idle, IL

I want to compare the online in file1 and OL in file2. But as you can see its seperated with a comma. If the online word in file1 change to offline or idle the output should be invalid and same as on file2. Thanks in advance guys!Any help would be appreciated. Cheers

Here you some points.

File3 looks like good candidate to extract it to the Map<K, V> , just place first value as key and second as value.

Read file1 and file2 by lines and split them by comma. Like:

try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    String line;
    while ((line = br.readLine()) != null) {
       String[] values = line.split(",");
       // extract values by indexes and use created map from file3 to validate the input.
    }
}

Validation logic:

Map<String, String> map; // Assuming you populate it with file3 content and it contains online->OL, offline->OF etc. ;
void validate(String valueFromFile1, String valueFromFile2) {
    if (map.containsKey(valueFromFile1) && map.get(valueFromFile1).equals(valueFromFile2)) {
        // Valid;
    } else {
        // invalid
    }

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