• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

Reading Text Files

fadd2

Exploit Expert
F Rep
0
0
0
Rep
0
F Vouches
0
0
0
Vouches
0
Posts
99
Likes
102
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
Introduction:
This page will teach you about reading from text files in Java.

What is used for reading from text files?
There are many ways to read from text files in Java but the way I will talk about is using BufferedReader with FileReaders. Because of this we need to import two namespaces as well as two extra namespaces used for separate exceptions;

  1. import

    java.io.BufferedReader

    ;
  2. import

    java.io.FileNotFoundException

    ;
  3. import

    java.io.FileReader

    ;
  4. import

    java.io.IOException

    ;

The Structure:
To read from a file we first need the file path of the text file as a string, we need the four imports listed above, and you will also need knowledge of Try and Catch statements, as well as Do While statements.

Examples:
The first thing we need to do is create a simple Try and Catch statement:

  1. try

    {
  2. }

    catch

    (

    FileNotFoundException

    e1)

    {
  3. e1.printStackTrace

    (

    )

    ;
  4. }

The statement throws a FileNotFoundException (which pretty simple and self-explanatory) but it handles the error if the file is not found that is trying to be read by the program. Now we need to create a BufferedReader from a FileReader of the text file path (this will essentially just grab the file for use)...

  1. try

    {
  2. BufferedReader

    br =

    new

    BufferedReader

    (

    new

    FileReader

    (

    "C:/Users/Josh/Desktop/read.txt"

    )

    )

    ;
  3. }

    catch

    (

    FileNotFoundException

    e1)

    {
  4. e1.printStackTrace

    (

    )

    ;
  5. }

Next we want to create a new String variable and initiate it as a blank String (to avoid an error) and then we want to read each line of the BufferedReader text file and add each line to the "line" string variable. This will be done through a Do While statement...

This is also encased in a Try and Catch statement (IO Exception) just in case there is a problem with the file (a line is blank etc). Here is the full source...

  1. try

    {
  2. BufferedReader

    br =

    new

    BufferedReader

    (

    new

    FileReader

    (

    "C:/Users/Josh/Desktop/read.txt"

    )

    )

    ;
  3. String

    line =

    ""

    ;
  4. try

    {
  5. do

    {
  6. line +=

    br.readLine

    (

    )

    ;
  7. }

    while

    (

    br.readLine

    (

    )

    !=

    null

    )

    ;
  8. }

    catch

    (

    IOException

    e)

    {
  9. e.printStackTrace

    (

    )

    ;
  10. }
  11. System

    .out

    .println

    (

    line)

    ;
  12. }

    catch

    (

    FileNotFoundException

    e1)

    {
  13. e1.printStackTrace

    (

    )

    ;
  14. }

Which gives the output which is contained in our "read.txt" file:

This is our read text file.

Finished!

Book traversal links for Reading Text Files

  • ‹ Object Oriented Programming (Theory)
  • Up
  • Return Keyword ›

 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,499

349,596

349,606

Top