4 Replies - 1435 Views - Last Post: 25 May 2011 - 04:27 AM Rate Topic: -----

#1 Mavsstarter21   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 12-May 11

EOFException on File and Object Input Streams/FileChooser

Posted 25 May 2011 - 02:01 AM

Edit: Sorry about the misleading title. I solved my EOFException problem but am still faced with the FileChooser Filter Problem.


My program begins by allowing the chooser to select a file using a FileChooser Dialog. The filter I have set only allows .sgo files. But it does not display .sgo files when I compile and run the program and open the FileChooser Dialog (which means it only shows directories). However, it does say that "SGO" files are the only files allowed and only accepts .sgo when I manually enter the names of valid .sgo files.

The .sgo file contains only one object(which contains an array of other objects).

What is wrong in my code that is not allowing the FileChooser Dialog to show any files, when I want it to show only .sgo files?




openingDialogJButton2.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e3)  {
    	
    	if (fc == null) {
            fc = new JFileChooser();

	    //Add a sgo file filter and disable the default
	   
            ExtensionFileFilter filter = new ExtensionFileFilter("SGO", ".sgo");
            fc.addChoosableFileFilter(filter);
            fc.setAcceptAllFileFilterUsed(false);

	    
        }

        //Show it.
        int returnVal = fc.showDialog(TryTabbedObjectOutputWithActionListeners.this,
                                      "Open");

        //Process the results.     
            if (returnVal == JFileChooser.APPROVE_OPTION) {
           	 try {
   				FileInputStream fIS = new FileInputStream(fc.getSelectedFile());
   			ObjectInputStream oIS = new ObjectInputStream(fIS);     //line 195
           	
   			Guide = (ObjectGuide) oIS.readObject();
           	 jLabel6.setText(Guide.getNameOfGuide());
           	 
           	 
           	 } catch (FileNotFoundException ex) {
           		JOptionPane.showMessageDialog(newGuideNamePanel,
   					    "The File was not found.",
   					    "File error1",
   					    JOptionPane.ERROR_MESSAGE);

   				System.exit(1);
   				ex.printStackTrace();
   			} catch (IOException e) {
   				JOptionPane.showMessageDialog(newGuideNamePanel,
   					    "Corrupt File Error.",
   					    "File error2",
   					    JOptionPane.ERROR_MESSAGE);

   				//System.exit(1);
   				e.printStackTrace();
   				
   			} catch (ClassNotFoundException e) {

   				JOptionPane.showMessageDialog(newGuideNamePanel,
   					    "Class Not Found Error.",
   					    "File error2",
   					    JOptionPane.ERROR_MESSAGE);

   				System.exit(1);
   				e.printStackTrace();
   			}
       
            
        //Reset the file chooser for the next time it's shown.
        fc.setSelectedFile(null);
       OpeningDialog.setVisible(false);
       
    }

This post has been edited by Mavsstarter21: 25 May 2011 - 02:07 AM


Is This A Good Question/Topic? 0
  • +

Replies To: EOFException on File and Object Input Streams/FileChooser

#2 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: EOFException on File and Object Input Streams/FileChooser

Posted 25 May 2011 - 02:09 AM

ExtensionFileFilter doesn't seem to be in the Java API. Is it something you wrote yourself? If so could you post the code please?
Was This Post Helpful? 0
  • +
  • -

#3 Mavsstarter21   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 12-May 11

Re: EOFException on File and Object Input Streams/FileChooser

Posted 25 May 2011 - 02:51 AM

Oh yes, you are right I apologize.


import java.io.File;

import javax.swing.filechooser.FileFilter;

class ExtensionFileFilter extends FileFilter {
	  String description;

	  String extensions[];

	  public ExtensionFileFilter(String description, String extension) {
	    this(description, new String[] { extension });
	  }

	  public ExtensionFileFilter(String description, String extensions[]) {
	    if (description == null) {
	      this.description = extensions[0];
	    } else {
	      this.description = description;
	    }
	    this.extensions = (String[]) extensions.clone();
	    toLower(this.extensions);
	  }

	  private void toLower(String array[]) {
	    for (int i = 0, n = array.length; i < n; i++) {
	      array[i] = array[i].toLowerCase();
	    }
	  }

	  public String getDescription() {
	    return description;
	  }

	  public boolean accept(File file) {
	    if (file.isDirectory()) {
	      return true;
	    } else {
	      String path = file.getAbsolutePath().toLowerCase();
	      for (int i = 0, n = extensions.length; i < n; i++) {
	        String extension = extensions[i];
	        if ((path.endsWith(extension) && (path.charAt(path.length() - extension.length() - 1)) == '.')) {
	          return true;
	        }
	      }
	    }
	    return false;
	  }
	}

Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: EOFException on File and Object Input Streams/FileChooser

Posted 25 May 2011 - 04:09 AM

Quote

                if ((path.endsWith(extension) 
			    && ((path.charAt(path.length() - extension.length() - 1)) == '.'))) {
                    return true;
                }



The second boolean clause is incorrect. It would only return files named x..sgo

You can get rid of it - if it were correct, it would be redundant
Was This Post Helpful? 1
  • +
  • -

#5 Mavsstarter21   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 12-May 11

Re: EOFException on File and Object Input Streams/FileChooser

Posted 25 May 2011 - 04:27 AM

View Postg00se, on 25 May 2011 - 04:09 AM, said:

Quote

                if ((path.endsWith(extension) 
			    && ((path.charAt(path.length() - extension.length() - 1)) == '.'))) {
                    return true;
                }



The second boolean clause is incorrect. It would only return files named x..sgo

You can get rid of it - if it were correct, it would be redundant


Ah, good catch. Simply deleting the "." before the "sgo" in the actionlistener got the job done. Much appreciation.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1