Read Data from a Text File using Java
Submitted by donbermoy on Monday, May 19, 2014 - 14:17.
Language
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.
4. In your main, initialize the data.txt that you have created a while ago. Initialize also StringBuffer contents and BufferedReader reader to 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.
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.
6. Lastly, display the contents of the data.txt file using toString method.
Output:
Here's the full code of this tutorial:
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
- import java.io.*;
- try
- {
- // repeat until all lines is read
- while ((text = reader.readLine()) != null)
- {
- contents.append(text)
- "line.separator"));
- }
- }
- {
- e.printStackTrace();
- {
- e.printStackTrace();
- }
- import java.io.*;
- public class readFiles
- {
- {
- try
- {
- // repeat until all lines is read
- while ((text = reader.readLine()) != null)
- {
- contents.append(text)
- "line.separator"));
- }
- {
- e.printStackTrace();
- {
- e.printStackTrace();
- }
- // show file contents here
- }
- }
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.
Add new comment
- 340 views