It's not a secret anymore!
The moose likes Java in General and the fly likes How to read a .txt file and open its contents Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to read a .txt file and open its contents" Watch "How to read a .txt file and open its contents" New topic

How to read a .txt file and open its contents

Zahra Vir

Joined: Dec 04, 2004
Posts: 4

I really need help. I am trying to display the contents of a text file by having an openFile() method.

Here is my code so far:

private void openFile()
JFileChooser chooser = new JFileChooser();

int result = chooser.showOpenDialog(this);

if(result == JFileChooser.CANCEL_OPTION)

//obtain selected file
File fileName = chooser.getSelectedFile();

//Buffered Reader
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String empData = reader.readLine();
catch(IOException ioException)
JOptionPane.showMessageDialog(this, "FILE ERROR", "FILE ERROR", JOptionPane.ERROR_MESSAGE);
This is just a small portion of the code from a much larger problem. If anyone can help me sort this out I am willing to send the whole file.

[ December 04, 2004: Message edited by: Zahra Vir ]
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

Welcome to JavaRanch!

What problem are you having with the code? The only thing I see offhand is that reading from the file should be done in a loop. Otherwise, you only get the first line.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Use a reader object like BufferedReader

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Zahra Vir

Joined: Dec 04, 2004
Posts: 4
thanks guys.

i still have the same problem, however, i have modified my code somewhat to get the following...

while (input != null)
StringTokenizer tokenizer = new StringTokenizer(input);

//Sets the ID token
int id = Integer.parseInt(tokenizer.nextToken());
//Sets the Name token
String name = tokenizer.nextToken();
//Sets the Phone Number Token
String phoneNo = tokenizer.nextToken();
//Sets the Years token
int years = Integer.parseInt(tokenizer.nextToken());
//Sets the Title token
String title = tokenizer.nextToken();

//Now, depending on what the title is, we can find out what additional info is needed
if (title.equals("Professor"))
int paper;

if (paperField.getText().equals("")) paper = 0;
else paper = Integer.parseInt(paperField.getText());

else if (title.equals("Manager"))


else if (title.equals("Engineer"))


//The next record can now be read, and the same process will continue
//input = reader.readLine();
I agree. Here's the link:
subject: How to read a .txt file and open its contents
jQuery in Action, 3rd edition