Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2007
    Posts
    9
    Rep Power
    0

    Red face 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();
    }
    }
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java 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:
    Java Code:
    String line;
    while((line = br.readLine()) != -1) {
        openAction.notes.append(line+"\n");
    }

  3. #3
    Join Date
    Jun 2007
    Posts
    9
    Rep Power
    0

Similar Threads

  1. Reading data form Excel using Jdbc (example)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-13-2008, 11:23 AM
  2. Reading Binary File and Changing data
    By janakiram.attuluri in forum Advanced Java
    Replies: 1
    Last Post: 12-21-2007, 10:10 AM
  3. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 AM
  4. Reading file data that contains no spaces
    By jdepue in forum Advanced Java
    Replies: 1
    Last Post: 08-01-2007, 04:58 AM
  5. Reading FileInput data
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 06:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •