johnwillard45
System Load Balancer
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.
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
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
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.
- 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.
- File
file =
new
File
(
"data.txt"
)
;
- StringBuffer
contents =
new
StringBuffer
(
)
;
- 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.
- try
- {
- reader =
new
BufferedReader
(
new
FileReader
(
file)
)
;
- String
text =
null
;
- // repeat until all lines is read
- while
(
(
text =
reader.readLine
(
)
)
!=
null
)
- {
- contents.append
(
text)
- .append
(
System
.getProperty
(
- "line.separator"
)
)
;
- }
- }
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.
- catch
(
FileNotFoundException
e)
- {
- e.printStackTrace
(
)
;
- }
catch
(
IOException
e)
- {
- e.printStackTrace
(
)
;
- }
6. Lastly, display the contents of the data.txt file using toString method.
- System
.out
.println
(
contents.toString
(
)
)
;
Output:

Here's the full code of this tutorial:
- import
java.io.*
;
- public
class
readFiles
- {
- public
static
void
main(
String
[
]
args)
- {
- File
file =
new
File
(
"data.txt"
)
;
- StringBuffer
contents =
new
StringBuffer
(
)
;
- BufferedReader
reader =
null
;
- try
- {
- reader =
new
BufferedReader
(
new
FileReader
(
file)
)
;
- String
text =
null
;
- // repeat until all lines is read
- while
(
(
text =
reader.readLine
(
)
)
!=
null
)
- {
- contents.append
(
text)
- .append
(
System
.getProperty
(
- "line.separator"
)
)
;
- }
- }
catch
(
FileNotFoundException
e)
- {
- e.printStackTrace
(
)
;
- }
catch
(
IOException
e)
- {
- e.printStackTrace
(
)
;
- }
- // show file contents here
- System
.out
.println
(
contents.toString
(
)
)
;
- }
- }
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.