简体   繁体   中英

How to Filter a txt file in java

As input i have a File containing a list of rooms. Each room has the following information: (1 line of the file)

ID;name;phonenumber;PIN e.g.:
1234;A 0.12;1234;123456789 (ID is same as [first] phonenumber)
2345;A 0.12;2345;123456789 (A room can have more than 1 phonenumber - in a new line like in the example)

Now i need a java program which returns a list like this:

 Example Input: 1127,A 0.01,1127,1 2476,A 0.01,2476,1 2309,A 0.01,2309,1 2306,A 0.01,2306,1 2706,A 0.01,2706,1 2757,A 0.01,2757,1 Generates Output: 1127,A 0.01,1127,2476,2309,2306,2706,2757,1

ID;name;phonenumber1;...;phonenumberN;PIN

This code (without main function) adds a second phone number to the existing one, if the rooms from 2 lines are the same.

public static String duplicateNumbers(String s, String x) {

    String result = s;
    if(compareString(s, x)) {
         result = result.substring(0, 21)
                 + x.charAt(21) + x.charAt(22) + x.charAt(23) + x.charAt(24) 
                 + result.substring(20); 
    }
    return result;
}

public static boolean compareString(String s, String x) {
    for(int i = 5; i < 10; i++) {
        if(s.charAt(i) != x.charAt(i)) return false;
    }
    return true;
}

ß

Try This:

String fileName = "D:/test.txt";

try (Stream<String> stream = Files.lines(Paths.get(fileName))) {

  stream.forEach(System.out::println);

} catch (IOException e) {
  e.printStackTrace();
}

I hope this is gonna be helpful for what you need. I used Apache Common CSV. You can find more info here https://www.baeldung.com/apache-commons-csv

ID;name;phonenumber;PIN;
    1127;A 0.01;1127;1;
    2476;A 0.01;2476;1;
    2309;A 0.01;2309;1;
    2306;A 0.01;2306;1;
    2706;A 0.01;2706;1;
    2757;A 0.01;2757;1;
    2722;B 0.01;2722;1;
    2723;B 0.01;2723;1;

@Test public void testForAlphaOmega() throws IOException { @Getter class ROOM { String id; String name; String phoneNumber; String pin; private ROOM(String id, String name, String phoneNumber, String pin){ this.id = id; this.name = name; this.phoneNumber = phoneNumber; this.pin = pin; } private ROOM(CSVRecord record){ this.id = record.get("ID"); this.name = record.get("name"); this.phoneNumber = record.get("phoneNumber"); this.pin = record.get("PIN"); } } try ( Reader reader = Files.newBufferedReader(Paths.get("....PATH_OF_FILE")); CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT .withHeader("ID", "name", "phoneNumber", "PIN") .withIgnoreHeaderCase() .withFirstRecordAsHeader() .withDelimiter(';') .withTrim()) ) { List<ROOM> rooms = new ArrayList<>(); csvParser.getRecords().stream().map(ROOM::new).collect(groupingBy(ROOM::getName)).forEach((key,val) -> rooms.add(new ROOM(val.get(0).getId(), val.get(0).getName(), val.stream().map(ROOM::getPhoneNumber).collect(Collectors.joining(",")),val.get(0).getPin()))); rooms.forEach(item -> System.out.println(item.getId() + ";" + item.getName() +";" + item.getPhoneNumber() + ";" + item.getPin())); } }

\n    2722;B 0.01;2722,2723;1 \n    1127;A 0.01;1127,2476,2309,2306,2706,2757;1 \n    

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