This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Reading in a File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading in a File" Watch "Reading in a File" New topic

Reading in a File

Jax Laakso

Joined: Dec 07, 2001
Posts: 23
I can't seem to remember how to read in a .txt file into a java program. Help?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Not sure what you mean by reading a file into a java program. If you just want your java program to read a text file...
wrap a buffered reader around a file reader, and then use it's reaadline method to read one line at a time.
FileReader fr = new FileReader("Youfilename")
BufferedReader br = new Bufferedreader(fr)
String line = be.readLine()
while(line != null)
do whatever...
//read the next lien
line = be.readLine()

SCJP for the Java� 2 Platform

So much trouble in the world -- Bob Marley
I agree. Here's the link:
subject: Reading in a File
It's not a secret anymore!