• We just launched and are currently in beta. Join us as we build and grow the community.

Read Data from a Text File using Java

johnwillard45

System Load Balancer
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
55
Likes
101
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Today, I will teach you how to read data from a text file using Java. We're done with creating files and writing/appending files, so now we will proceed to reading of data from any files but here we just use the text files or .txt file extension.

So, now let's start this tutorial!

1. Open Notepad. Put any data in there, for example, i have write "Sourcecodester is the best!". Save it to the same folder with your java program and named it as data.txt.

2. Open JCreator or NetBeans and make a java program with a file name of readFiles.java.

3. Import java.io package. Hence we will use an input/output in creating files.

  1. import

    java.io.*

    ;

4. In your main, initialize the data.txt that you have created a while ago. Initialize also StringBuffer contents and BufferedReader reader to null.
  1. File

    file =

    new

    File

    (

    "data.txt"

    )

    ;
  2. StringBuffer

    contents =

    new

    StringBuffer

    (

    )

    ;
  3. BufferedReader

    reader =

    null

    ;

The StringBuffer class here is used to represent characters that can be changed or modified and the BufferedReader class here means that it reads from the input source into a buffer before passing it to another string.

5. Now, create a try and catch method. In your try method, do the following code. This will trigger to read all the data lines of data.txt.
  1. try
  2. {
  3. reader =

    new

    BufferedReader

    (

    new

    FileReader

    (

    file)

    )

    ;
  4. String

    text =

    null

    ;

  5. // repeat until all lines is read
  6. while

    (

    (

    text =

    reader.readLine

    (

    )

    )

    !=

    null

    )
  7. {
  8. contents.append

    (

    text)
  9. .append

    (

    System

    .getProperty

    (
  10. "line.separator"

    )

    )

    ;
  11. }
  12. }

In your Catch method, prefer to catch IOException then use printStackTrace() method. This will help to trace the exception and identify which method causes the bug. And also the FileNotFoundException if the file is not found under the same folder of the program.
  1. catch

    (

    FileNotFoundException

    e)
  2. {
  3. e.printStackTrace

    (

    )

    ;
  4. }

    catch

    (

    IOException

    e)
  5. {
  6. e.printStackTrace

    (

    )

    ;
  7. }

6. Lastly, display the contents of the data.txt file using toString method.

  1. System

    .out

    .println

    (

    contents.toString

    (

    )

    )

    ;


Output:

read.png


Here's the full code of this tutorial:

  1. import

    java.io.*

    ;
  2. public

    class

    readFiles
  3. {
  4. public

    static

    void

    main(

    String

    [

    ]

    args)
  5. {
  6. File

    file =

    new

    File

    (

    "data.txt"

    )

    ;
  7. StringBuffer

    contents =

    new

    StringBuffer

    (

    )

    ;
  8. BufferedReader

    reader =

    null

    ;

  9. try
  10. {
  11. reader =

    new

    BufferedReader

    (

    new

    FileReader

    (

    file)

    )

    ;
  12. String

    text =

    null

    ;

  13. // repeat until all lines is read
  14. while

    (

    (

    text =

    reader.readLine

    (

    )

    )

    !=

    null

    )
  15. {
  16. contents.append

    (

    text)
  17. .append

    (

    System

    .getProperty

    (
  18. "line.separator"

    )

    )

    ;
  19. }
  20. }

    catch

    (

    FileNotFoundException

    e)
  21. {
  22. e.printStackTrace

    (

    )

    ;
  23. }

    catch

    (

    IOException

    e)
  24. {
  25. e.printStackTrace

    (

    )

    ;
  26. }

  27. // show file contents here
  28. System

    .out

    .println

    (

    contents.toString

    (

    )

    )

    ;
  29. }
  30. }

Hope this program helps! :)

Best Regards,

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

440,010

316,559

316,568

Top