Perogram Terminates After Reading First Line Java
Disclosure: This article may incorporate affiliate links. When you purchase, we may earn a small commission.
How to read file line by line in Coffee - BufferedReader Scanner Example Tutorial
Line past Line reading in Java using BufferedReader and Scanner
In that location are multiple means to read files line by line in Java. A nigh simple instance of reading file line past line is using BufferedReader which provides the method readLine() for reading files. Apart from generics, enum and varargs Coffee 1.five has also introduced several new classes in Java API one of the utility classes is Scanner , which tin also be used to read any file line by line in Java. Though BufferedReader is available in Java from JDK one.1, java.util.Scanner provides more utility methods compared to BufferedReader . Scanner has method like hasNextLine() and nextLine() to facilitate line by line reading of file's contents.
The nextLine() returns String like to readLine() but the scanner has more than utility methods like nextInt() , nextLong() which tin can exist used to directly read numbers from a file instead of converting Cord to Integer or other Number classes.
Past the fashion, a Scanner is rather a new approach for reading a file, and BufferedReader is the standard one. This is my half-dozenth tutorial on Java IO, In the last couple of post, we take seen creating file and directory in Coffee, parsing XML files using DOM and how to read backdrop file in Java. In this mail, we volition focus on reading the file line by line in Java using both BufferedReader and Scanner .
Reading file line by line in Java - BufferedReader Example
In this example, we are using BufferedReader for reading file content line by line. BufferedReader needs an InputStream which is a FileInputStream in this case and readLine() returns a value of line or null if the terminate of Stream has reached. The line is terminated with line terminator e.1000. \north or \r
import java.io.BufferedReader ; /**
import java.io.FileInputStream ;
import java.io.FileNotFoundException ;
import java.io.IOException ;
import java.io.InputStreamReader ;
import java.util.logging.Level ;
import java.util.logging.Logger ;
* BufferedReader and Scanner tin can be used to read line by line from whatever File or
* panel in Java.
* This Java plan demonstrate line by line reading using BufferedReader in Coffee
*
* @author Javin Paul public static void main( String args[]) { //reading file line by line in Coffee using BufferedReader try { Organisation.out.println ( "Reading File line by line using BufferedReader" ) ; String line = reader.readLine () ; } grab ( FileNotFoundException ex) { } finally { Output:
*/
public class BufferedReaderExample {
FileInputStream fis = null ;
BufferedReader reader = null ;
fis = new FileInputStream ( "C:/sample.txt" ) ;
reader = new BufferedReader ( new InputStreamReader (fis)) ;
while (line != zilch ){
System.out.println (line) ;
line = reader.readLine () ;
}
Logger.getLogger (BufferedReaderExample. class .getName ()).log ( Level.Severe, null , ex) ;
} catch ( IOException ex) {
Logger.getLogger (BufferedReaderExample. class .getName ()).log ( Level.SEVERE, null , ex) ;
try {
reader.close () ;
fis.shut () ;
} catch ( IOException ex) {
Logger.getLogger (BufferedReaderExample. class .getName ()).log ( Level.Severe, nothing , ex) ;
}
}
}
Reading File line by line using BufferedReader
commencement line in file
second line
third line
fourth line
fifth line
final line in file
while using FileInputStream or whatever IO Reader don't forget to close the stream in finally block so that file descriptor associated with this operation get released. If not airtight properly Java plan may leak file descriptors which is a limited resource and in worst example program will non be able to open whatsoever new file. If yous are Java 7 and so you can use Automatic resource direction or ARM blocks to let those resources automatically get airtight past Java.
Reading file line by line in Java - Scanner Example
Scanner is new addition in Coffee 1.5 along with several other changes like auto-boxing and has been a preferred choice for reading inputs from the panel. Since Scanner is able to read from InputStream , it can also exist used to read from text File and information technology provide many utility methods to read line by line contents using nextLine() or nextInt() etc.
import java.io.FileInputStream ; /**
import coffee.io.FileNotFoundException ;
import java.util.Scanner ;
* @author Javin Paul
* Coffee program to read file line past line using Scanner. Scanner is a rather new
* utility class in Coffee and introduced in JDK 1.5 and preferred fashion to read input
* from console. public static void principal( String args[]) throws FileNotFoundException{ //Scanner Instance - read file line past line in Java using Scanner //reading file line by line using Scanner in Java while (scanner.hasNextLine ()){ scanner.shut () ; } Output:
*/
public class ScannerExample {
FileInputStream fis = new FileInputStream ( "C:/sample.txt" ) ;
Scanner scanner = new Scanner (fis) ;
System.out.println ( "Reading file line past line in Java using Scanner" ) ;
Organization.out.println (scanner.nextLine ()) ;
}
}
Reading file line by line in Java using Scanner
showtime line in file
second line
tertiary line
quaternary line
fifth line
last line in file
That'due south all on How to read files line by line in Java. We take seen two simple examples of line-by-line reading using BufferedReader and Scanner . If you look code with Scanner looks cleaner and give a broad multifariousness of data conversion method it supports, it simply a handy choice for reading file line past line in Coffee.
Other Java IO tutorials from the Javarevisited blog
Source: https://javarevisited.blogspot.com/2012/07/read-file-line-by-line-java-example-scanner.html
0 Response to "Perogram Terminates After Reading First Line Java"
Enviar um comentário