Read Text File and Create Body Array Java
In this article, you lot'll larn how to read a text file or binary (epitome) file in Java using diverse classes and utility methods provided by Java like BufferedReader
, LineNumberReader
, Files.readAllLines
, Files.lines
, BufferedInputStream
, Files.readAllBytes
, etc.
Permit's expect at each of the different ways of reading a file in Java with the assist of examples.
Java read file using BufferedReader
BufferedReader is a simple and performant way of reading text files in Coffee. It reads text from a character-input stream. It buffers characters to provide efficient reading.
import java.io. BufferedReader ; import java.io. IOException ; import java.nio.charset. Charset ; import coffee.nio.charset. StandardCharsets ; import java.nio.file. Files ; import coffee.nio.file. Path ; import java.nio.file. Paths ; public class BufferedReaderExample { public static void principal ( String [ ] args) { Path filePath = Paths . get ( "demo.txt" ) ; Charset charset = StandardCharsets .UTF_8; try ( BufferedReader bufferedReader = Files . newBufferedReader (filePath, charset) ) { String line; while ( (line = bufferedReader. readLine ( ) ) != naught ) { System .out. println (line) ; } } catch ( IOException ex) { Arrangement .out. format ( "I/O error: %southward%north" , ex) ; } } }
Java read file line by line using Files.readAllLines()
Files.readAllLines() is a utility method of the Java NIO's Files class that reads all the lines of a file and returns a Listing<String>
containing each line. Information technology internally uses BufferedReader to read the file.
import java.io. IOException ; import java.nio.charset. Charset ; import java.nio.charset. StandardCharsets ; import java.nio.file. Files ; import java.nio.file. Path ; import java.nio.file. Paths ; import java.util. Listing ; public course FilesReadAllLinesExample { public static void main ( String [ ] args) { Path filePath = Paths . get ( "demo.txt" ) ; Charset charset = StandardCharsets .UTF_8; effort { List < String > lines = Files . readAllLines (filePath, charset) ; for ( Cord line: lines) { System .out. println (line) ; } } catch ( IOException ex) { System .out. format ( "I/O error: %south%n" , ex) ; } } }
Coffee read file line by line using Files.lines()
Files.lines() method reads all the lines from a file equally a Stream
. You can utilise Stream API methods like forEach
, map
to work with each line of the file.
import coffee.io. IOException ; import java.nio.charset. Charset ; import java.nio.charset. StandardCharsets ; import java.nio.file. Files ; import java.nio.file. Path ; import java.nio.file. Paths ; public class FilesLinesExample { public static void main ( String [ ] args) { Path filePath = Paths . go ( "demo.txt" ) ; Charset charset = StandardCharsets .UTF_8; try { Files . lines (filePath, charset) . forEach ( System .out:: println ) ; } grab ( IOException ex) { System .out. format ( "I/O error: %southward%due north" , ex) ; } } }
Java read file line by line using LineNumberReader
LineNumberReader is a buffered grapheme-stream reader that keeps rails of line numbers. You tin can use this course to read a text file line by line.
import java.io. * ; import coffee.nio.charset. Charset ; import coffee.nio.charset. StandardCharsets ; import coffee.nio.file. Files ; import java.nio.file. Path ; import java.nio.file. Paths ; public class LineNumberReaderExample { public static void primary ( String [ ] args) { Path filePath = Paths . get ( "demo.txt" ) ; Charset charset = StandardCharsets .UTF_8; endeavor ( BufferedReader bufferedReader = Files . newBufferedReader (filePath, charset) ; LineNumberReader lineNumberReader = new LineNumberReader (bufferedReader) ) { String line; while ( (line = lineNumberReader. readLine ( ) ) != null ) { System .out. format ( "Line %d: %southward%north" , lineNumberReader. getLineNumber ( ) , line) ; } } catch ( IOException ex) { Arrangement .out. format ( "I/O error: %s%n" , ex) ; } } }
Java read binary file (image file) using BufferedInputStream
All the examples presented in this article so far read textual data from a character-input stream. If you're reading a binary data such as an image file then you need to employ a byte-input stream.
BufferedInputStream lets you read raw stream of bytes. It also buffers the input for improving operation.
import java.io. * ; import java.nio.file. Files ; import java.nio.file. Paths ; public class BufferedInputStreamImageCopyExample { public static void main ( String [ ] args) { try ( InputStream inputStream = Files . newInputStream ( Paths . get ( "sample.jpg" ) ) ; BufferedInputStream bufferedInputStream = new BufferedInputStream (inputStream) ; OutputStream outputStream = Files . newOutputStream ( Paths . get ( "sample-copy.jpg" ) ) ; BufferedOutputStream bufferedOutputStream = new BufferedOutputStream (outputStream) ) { byte [ ] buffer = new byte [ 4096 ] ; int numBytes; while ( (numBytes = bufferedInputStream. read (buffer) ) != - 1 ) { bufferedOutputStream. write (buffer, 0 , numBytes) ; } } catch ( IOException ex) { System .out. format ( "I/O mistake: %s%n" , ex) ; } } }
Java read file into []byte using Files.readAllBytes()
If you desire to read the entire contents of a file in a byte assortment so you lot can use the Files.readAllBytes() method.
import com.lord's day.org.apache.xpath.internal.operations. String ; import java.io. IOException ; import coffee.nio.file. Files ; import java.nio.file. Paths ; public class FilesReadAllBytesExample { public static void primary ( String [ ] args) { effort { byte [ ] information = Files . readAllBytes ( Paths . become ( "demo.txt" ) ) ; // Use byte data } catch ( IOException ex) { System .out. format ( "I/O fault: %due south%n" , ex) ; } } }
Source: https://www.callicoder.com/java-read-file/
0 Response to "Read Text File and Create Body Array Java"
Post a Comment