Java Program to Store and Read Objects From a File

How to read a File in Java

In this article, you'll acquire how to read a text file or binary (image) file in Java using various classes and utility methods provided by Coffee similar BufferedReader, LineNumberReader, Files.readAllLines, Files.lines, BufferedInputStream, Files.readAllBytes, etc.

Let'due south await at each of the unlike ways of reading a file in Java with the assistance of examples.

Java read file using BufferedReader

BufferedReader is a unproblematic and performant way of reading text files in Coffee. Information technology reads text from a character-input stream. It buffers characters to provide efficient reading.

                          import              coffee.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              coffee.nio.file.                            Paths              ;              public              class              BufferedReaderExample              {              public              static              void              main              (              String              [              ]              args)              {              Path              filePath              =              Paths              .              become              (              "demo.txt"              )              ;              Charset              charset              =              StandardCharsets              .UTF_8;              try              (              BufferedReader              bufferedReader              =              Files              .              newBufferedReader              (filePath,              charset)              )              {              String              line;              while              (              (line              =              bufferedReader.              readLine              (              )              )              !=              zilch              )              {              System              .out.              println              (line)              ;              }              }              take hold of              (              IOException              ex)              {              System              .out.              format              (              "I/O mistake: %s%n"              ,              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 List<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.                            List              ;              public              form              FilesReadAllLinesExample              {              public              static              void              principal              (              String              [              ]              args)              {              Path              filePath              =              Paths              .              get              (              "demo.txt"              )              ;              Charset              charset              =              StandardCharsets              .UTF_8;              try              {              Listing                              <                String                >                            lines              =              Files              .              readAllLines              (filePath,              charset)              ;              for              (              String              line:              lines)              {              System              .out.              println              (line)              ;              }              }              catch              (              IOException              ex)              {              Arrangement              .out.              format              (              "I/O error: %s%n"              ,              ex)              ;              }              }              }                      

Coffee read file line by line using Files.lines()

Files.lines() method reads all the lines from a file as a Stream. Y'all can use Stream API methods similar forEach, map to work with each line of the file.

                          import              java.io.                            IOException              ;              import              java.nio.charset.                            Charset              ;              import              java.nio.charset.                            StandardCharsets              ;              import              coffee.nio.file.                            Files              ;              import              java.nio.file.                            Path              ;              import              java.nio.file.                            Paths              ;              public              class              FilesLinesExample              {              public              static              void              principal              (              Cord              [              ]              args)              {              Path              filePath              =              Paths              .              get              (              "demo.txt"              )              ;              Charset              charset              =              StandardCharsets              .UTF_8;              effort              {              Files              .              lines              (filePath,              charset)              .              forEach              (              System              .out::              println              )              ;              }              grab              (              IOException              ex)              {              System              .out.              format              (              "I/O mistake: %s%n"              ,              ex)              ;              }              }              }                      

Java read file line by line using LineNumberReader

LineNumberReader is a buffered character-stream reader that keeps runway of line numbers. You tin use this class to read a text file line by line.

                          import              java.io.                            *              ;              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              LineNumberReaderExample              {              public              static              void              chief              (              String              [              ]              args)              {              Path              filePath              =              Paths              .              get              (              "demo.txt"              )              ;              Charset              charset              =              StandardCharsets              .UTF_8;              try              (              BufferedReader              bufferedReader              =              Files              .              newBufferedReader              (filePath,              charset)              ;              LineNumberReader              lineNumberReader              =              new              LineNumberReader              (bufferedReader)              )              {              String              line;              while              (              (line              =              lineNumberReader.              readLine              (              )              )              !=              aught              )              {              System              .out.              format              (              "Line %d: %s%n"              ,              lineNumberReader.              getLineNumber              (              )              ,              line)              ;              }              }              catch              (              IOException              ex)              {              System              .out.              format              (              "I/O error: %south%north"              ,              ex)              ;              }              }              }                      

Java read binary file (image file) using BufferedInputStream

All the examples presented in this article so far read textual information from a character-input stream. If you're reading a binary data such as an prototype file then you need to use a byte-input stream.

BufferedInputStream lets you read raw stream of bytes. It also buffers the input for improving performance.

                          import              java.io.                            *              ;              import              java.nio.file.                            Files              ;              import              java.nio.file.                            Paths              ;              public              class              BufferedInputStreamImageCopyExample              {              public              static              void              main              (              String              [              ]              args)              {              endeavor              (              InputStream              inputStream              =              Files              .              newInputStream              (              Paths              .              get              (              "sample.jpg"              )              )              ;              BufferedInputStream              bufferedInputStream              =              new              BufferedInputStream              (inputStream)              ;              OutputStream              outputStream              =              Files              .              newOutputStream              (              Paths              .              go              (              "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 error: %s%n"              ,              ex)              ;              }              }              }                      

Coffee read file into []byte using Files.readAllBytes()

If you desire to read the entire contents of a file in a byte array and so you lot tin can utilise the Files.readAllBytes() method.

                          import              com.sun.org.apache.xpath.internal.operations.                            Cord              ;              import              java.io.                            IOException              ;              import              coffee.nio.file.                            Files              ;              import              java.nio.file.                            Paths              ;              public              form              FilesReadAllBytesExample              {              public              static              void              main              (              String              [              ]              args)              {              try              {              byte              [              ]              data              =              Files              .              readAllBytes              (              Paths              .              go              (              "demo.txt"              )              )              ;              // Utilize byte data              }              grab              (              IOException              ex)              {              System              .out.              format              (              "I/O error: %s%n"              ,              ex)              ;              }              }              }                      

durochermovenciought1948.blogspot.com

Source: https://www.callicoder.com/java-read-file/

0 Response to "Java Program to Store and Read Objects From a File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel