Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack (1) Thread Tools Display Modes
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 10-08-2007, 12:44 PM
Member
 
Join Date: Jun 2007
Posts: 7
ramachandran is on a distinguished road
Reading Data from a file
I am working on a project, creating a Notepad..
I want to read the text file from the disk...
My problem is that the forth coming code reads only some of the text files and throws exception while reading some files...
My second problem is that the forth coming code reads the text files by leaving the first line of the text files



import javax.swing.filechooser.FileFilter;
public class FileOpenClass extends Component {
MyNotepadWindow openAction;
ActionClass ActionClass;
public FileOpenClass(MyNotepadWindow openAction,ActionClass ActionClass)
{
this.ActionClass = ActionClass;
this.openAction = openAction;
}
public FileOpenClass(MyNotepadWindow openAction, String action,ActionClass ActionClass) {
this(openAction,ActionClass);
String selectedFile;
String[] textDocuments = new String[] {"ini","bat","txt"};
JFileChooser openChooser = new JFileChooser();
openChooser.setMultiSelectionEnabled(false);
BufferedReader br;

try
{
openChooser.addChoosableFileFilter(new DisplayFilters(textDocuments,
"Text Documents (*.ini,*.bat,*.txt)"));
int option = openChooser.showOpenDialog(new JFrame());
if(option == JFileChooser.APPROVE_OPTION) {
if(openChooser.getSelectedFile() !=null) {
selectedFile = openChooser.getSelectedFile().getName();
br = new BufferedReader(new FileReader(selectedFile));
openAction.notes.setText(" ");
while((br.readLine()) != -1) {
openAction.notes.append(br.readLine()+"\n");
}
openAction.setTitle(selectedFile + " - MyNotepad");
if(ActionClass.statusBar.isVisible()) {
ActionClass.statusBar.setText("you selected " + selectedFile);
}
}
else if (option == JFileChooser.CANCEL_OPTION) {
if(ActionClass.statusBar.isVisible()) {
ActionClass.statusBar.setText("you didn't select any file");
}
}

}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-08-2007, 09:21 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
// This line reads one line in the file. while((br.readLine()) != -1) { // This line reads the next line in the file. // If this readLine call hits the end_of_file // (-1 returned) it will throw an exception. openAction.notes.append(br.readLine()+"\n"); }
Solution? Save each line that you read and isolate the
reading to the while loop condition statement, like so:
Code:
String line; while((line = br.readLine()) != -1) { openAction.notes.append(line+"\n"); }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-24-2007, 09:22 AM
Member
 
Join Date: Jun 2007
Posts: 7
ramachandran is on a distinguished road
Thanking
Thanking for your kind help..
Its working well
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.java-forums.org/new-java/3299-reading-data-file.html
Posted By For Type Date
CrossFTP 1.37 Released This thread Refback 09-22-2007 09:47 PM

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading data form Excel using Jdbc (example) Java Tip Java Tips 0 02-13-2008 01:23 PM
Reading Binary File and Changing data janakiram.attuluri Advanced Java 1 12-21-2007 12:10 PM
Reading in data from file line by line bluekswing New To Java 1 10-02-2007 02:19 AM
Reading file data that contains no spaces jdepue Advanced Java 1 08-01-2007 06:58 AM
Reading FileInput data Eric Advanced Java 1 07-05-2007 08:59 AM


All times are GMT +3. The time now is 11:09 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org