Results 1 to 19 of 19
  1. #1
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Default FileInputStream Problems.

    How can I get a FileNotFoundException when Im using a JFileChooser to get the file path. The file is instantiated fine but the inputStream gives the exception.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You're doing something wrong, but without code, I'm not sure how we can know why. Please read my signature link about creating and posting an SSCCE.
    Last edited by Fubarable; 08-10-2010 at 08:16 PM.

  3. #3
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Default

    Java Code:
    private void initComponents(){
    
            
            MenuItem Open = new MenuItem("فتح ملف");
            MenuItem Exit = new MenuItem("خروج");
            Menu File = new Menu("ملف");
            Menu Edit = new Menu("تعديل");
            File.add(Open);
            File.add(Exit);
            Open.addActionListener(new ActionListener(){
    
                public void actionPerformed(ActionEvent e){OpenFileDialog dialog = new OpenFileDialog();
            javax.swing.JOptionPane.showMessageDialog(dialog, "File Opened");
            File file = dialog.chosenFile;
            FileInputStream stream = new FileInputStream(file);// error here
    
            GasWaterRecordsProcessor processor = new GasWaterRecordsProcessor(stream);
            gwpanel.flipPage(0);}
            });
    
            Exit.addActionListener(new ActionListener(){
    
                public void actionPerformed(ActionEvent e){System.exit(0);}
            });
    
           MenuBar bar = new MenuBar();
           bar.add(File);
           bar.add(Edit);
           super.setMenuBar(bar);
           Container pane = super.getContentPane();
           JTabbedPane tabs = new JTabbedPane();
           tabs.add(gwpanel, "الغاز والمياه");
           pane.add(tabs);
           super.setSize(800,470);
           super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Really not much to see

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Most of us do better with an SSCCE. Again, I urge you to read the link. Also helps if we can see the actual error messages.

    Luck!

  5. #5
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Default

    I really dont know how much more to tell you. I practically copy pasted from a guide

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by bayan View Post
    I really dont know how much more to tell you. I practically copy pasted from a guide
    I understand, and what you've posted may be enough for some here to help you, but me, I'm not smart enough, and I do much better if you produce an SSCCE, a small program that we can compile, run, and see your problem first hand, that has no code present unrelated to your problem, and that we can modify to hopefully find a solution. So if no one smarter posts a simple solution, then please read the link. Here it is again:
    SSCCE.

    Much luck!

  7. #7
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Default

    Oh its a program... I thought it was that how to write a question guide
    Hold on..

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    What happens when you do this:

    Java Code:
    FileInputStream stream = new FileInputStream(file.getAbsolutePath());
    kind regards,

    Jos

  9. #9
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Default

    Same error: Unreported error at java.io.FileNotFoundException

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by bayan View Post
    Same error: Unreported error at java.io.FileNotFoundException
    ... then you should print out the details for the selected File and see what happens; (the scenario works for me).

    kind regards,

    Jos

  11. #11
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Default

    Thanks Jos ill try that ill let you know

  12. #12
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Default

    Entering the pathname manually renders no exception. only with JFileChooser it does

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    If you can execute the program in a command prompt, you could copy the full contents of the screen here.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by bayan View Post
    Entering the pathname manually renders no exception. only with JFileChooser it does
    What does getAbsolutePath() return on the returned File? Are they the same?

    kind regards,

    Jos

  15. #15
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Default

    Im using netbeans ide i dont know i i can use command prompt

    @Jos Yes they are identical. I tried the absolute filepath and the relative non worked.

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by bayan View Post
    @Jos Yes they are identical. I tried the absolute filepath and the relative non worked.
    That's a bit of an ambiguous remark: did the absolute path work for you? If so, see my first reply in this thread.

    kind regards,

    Jos

  17. #17
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Default

    I saw somewhere that it might be open in another process thats why, but i cant see the process, thats why i came here. Anyway thanks for the help guys really appreciate it

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    How are you handling the FileNotFoundException?
    I see no try/catch in your code.
    There's usually more information than just the exception name.

    For example this code:
    Java Code:
    public class Scratch {
    
        public static void main (String[] args) {
            File f= new File("C:/blah.blh");
            FileReader fr = null;
            try {
                fr = new FileReader(f);
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } finally {
                if (fr != null) {
                    try {
                        fr.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    gives me this stack trace, which tells me the file name it has attempted to find:
    Java Code:
    java.io.FileNotFoundException: C:\blah.blh (The system cannot find the file specified)
    	at java.io.FileInputStream.open(Native Method)
    	at java.io.FileInputStream.<init>(FileInputStream.java:106)
    	at java.io.FileReader.<init>(FileReader.java:55)
    	at Scratch.main(Scratch.java:25)

  19. #19
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Default

    Amazingly it worked. I wanted to avoid try catch because variables in the statement cannot be read outside it. I encapsulated the entire block with try catch and it worked thanks a lot :).

    Now to find something that reads arabic excel files.......

    For those of you who dont know poi and JExcel support foreign characters only, not orientations. If an excel document was created with an arabic version for excel for example it will give an unreported error in IOException.

Similar Threads

  1. FileInputStream not recognising file
    By javabarn in forum New To Java
    Replies: 3
    Last Post: 07-08-2010, 10:52 PM
  2. Replies: 5
    Last Post: 02-02-2010, 10:06 AM
  3. FileInputStream
    By pachufir in forum New To Java
    Replies: 3
    Last Post: 12-11-2009, 06:07 PM
  4. FileOutputStream > int > FileInputStream
    By dudejonne in forum New To Java
    Replies: 11
    Last Post: 11-11-2009, 05:03 PM
  5. Empty FileInputStream..
    By dudejonne in forum New To Java
    Replies: 5
    Last Post: 11-08-2009, 09:21 PM

Posting Permissions

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