java read each lines in file
Scanner sc = null;
try {
File file = new File("myfile.txt"); // java.io.File
sc = new Scanner(file); // java.util.Scanner
String line;
while (sc.hasNextLine()) {
line = sc.nextLine();
// process the line
}
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
finally {
if (sc != null) sc.close();
} BufferedReader br = null;
try {
File file = new File("myfile.txt"); // java.io.File
FileReader fr = new FileReader(file); // java.io.FileReader
br = new BufferedReader(fr); // java.io.BufferedReader
String line;
while ((line = br.readLine()) != null) {
// process the line
}
}
catch(IOException e) { e.printStackTrace();}
finally
{
try { if (br != null) br.close(); }
catch(IOException e) { e.printStackTrace(); }
}
Also in Java:
- one line if statement java
- java binary tree
- nextchar in java
- java observable to observer stack overflow
- java code to get all leaf nodes of a xml
- how to do 4th root java
- funzione random in java
- even or odd in java
- java for
- check if string is null or empty java
- arrays.aslist.add
- javafx polygon
- java calculator code
- ternary operator java
- java singleton implementation
- java scanner input int
- how to create a linked list in java
- how to input in java
- fragment button nullpointerexception
- string to arraylist convert java
- indexof java
- how to break two loop in java
- java swing pic
- how to echo java_home in windows cmd