Advertisement
DulcetAirman

Find line in file

Jan 14th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.58 KB | None | 0 0
  1. package ch.claude_martin;
  2.  
  3. import java.io.IOException;
  4. import java.nio.file.Files;
  5. import java.nio.file.Path;
  6. import java.nio.file.Paths;
  7. import java.util.Optional;
  8.  
  9. public class SomeClass {
  10.  
  11.   public static void main(String... args) {
  12.     var filename = args[0];
  13.     var city= args[1];
  14.     Path path = Paths.get(filename);
  15.     try (var lines = Files.lines(path)) {
  16.       Optional<String> first = lines.filter(s -> s.contains(city)).findFirst();
  17.       System.out.println(first.orElse("City not found"));
  18.     } catch (IOException e) {
  19.       e.printStackTrace();
  20.     }
  21.   }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement