• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

How to read a .txt file and open its contents

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author & internet detective
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Zahra,
Use a reader object like BufferedReader

 
Zahra Vir
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
 
Yeah, but is it art? What do you think tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic