SHARE
TWEET

Find line in file

DulcetAirman Jan 14th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top