Skip to content

Instantly share code, notes, and snippets.

@oofnivek
Last active January 11, 2024 12:24
Show Gist options
  • Save oofnivek/ad7c1c7b4fb7f3a4436f43f87a058a63 to your computer and use it in GitHub Desktop.
Save oofnivek/ad7c1c7b4fb7f3a4436f43f87a058a63 to your computer and use it in GitHub Desktop.
Read file line by line
package org.example;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try {
BufferedReader reader = new BufferedReader(new FileReader("fruits.txt"));
String line = reader.readLine();
while(line != null){
System.out.println(line);
line = reader.readLine();
}
reader.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment