File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

How to read a .txt file and open its contents

Zahra Vir
Greenhorn

Joined: Dec 04, 2004
Posts: 4
Hi,

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)
return;

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

try
{
//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.

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

Joined: May 26, 2003
Posts: 30116
    
150

Zahra,
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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Zahra,
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
Greenhorn

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();
}
 
 
subject: How to read a .txt file and open its contents
 
Similar Threads
File IO
Does this make sense?
Opening & Saving Problem
Background Color for JFileChooser
Opening & Saving Problem