Saturday, May 20, 2023
HomeSoftware EngineeringLearn a File Line by Line in Java

Learn a File Line by Line in Java


If that you must learn a file line by line in Java, then you should utilize one of many following three (3) choices.

Choice 1

You need to use the FileReader and BufferedReader packages as follows:

File file = new File("./your/file.txt");

attempt (FileReader fr = new FileReader(file);
  BufferedReader br = new BufferedReader(fr);) {
  String line;
  whereas ((line = br.readLine()) != null) {
    System.out.println(line);
  }
} catch (IOException e) {
  e.printStackTrace();
}

Choice 2

You can even learn the strains utilizing Information and Paths as follows:

Path filePath = Paths.get("./some/listing", "file.txt");
 
attempt (Stream<String> strains = Information.strains( filePath )) {
  strains.forEach(System.out::println);
} 
catch (IOException e) {
  e.printStackTrace();
}

Choice 3

You can even use the FileUtils class from Apache Commons IO as follows:

File file = new File("./your/file.txt");

attempt {
  Listing<String> strains = FileUtils.readLines(file, Charset.defaultCharset());
} catch (IOException e) {
  e.printStackTrace();
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments