[英]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)
每个房间都有以下信息:(文件的1行)
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:现在我需要一个java程序,它返回一个这样的列表:
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 ID;姓名;电话号码1;...;电话号码N;PIN
This code (without main function) adds a second phone number to the existing one, if the rooms from 2 lines are the same.如果 2 条线路的房间相同,则此代码(不带主要功能)向现有电话号码添加第二个电话号码。
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.
我使用了 Apache Common CSV。 You can find more info here https://www.baeldung.com/apache-commons-csv
您可以在此处找到更多信息https://www.baeldung.com/apache-commons-csv
File.csv
文件.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())); } }
Result
结果
\n 2722;B 0.01;2722,2723;12722;B 0.01;2722,2723;1\n 1127;A 0.01;1127,2476,2309,2306,2706,2757;1
1127;A 0.01;1127,2476,2309,2306,2706,2757;1\n
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.