Results 1 to 5 of 5

Thread: Save file

  1. #1
    dejos456 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default Save file

    Hi, i'm making a texteditor in java but i have a problem with the save function.
    I'm using Netbeans and the build in GUI editor.

    I can save my textfile but only in another folder then my active folder. When i try to save in my current folder, it trows an IOException.

    I use a menu button to save, and it calls a jFileChooser to select the correct file.

    This is my code for the save function:

    Java Code:
    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
            int returnVal = jFileChooser2.showSaveDialog(this);
            if (returnVal == jFileChooser2.APPROVE_OPTION)
            {
                File fout = jFileChooser2.getSelectedFile();
                String text=jTextArea1.getText();
                String filename;
                filename=fout.getPath();
                boolean exists=(new File(filename)).exists();
                if(!exists)
                {System.out.println("Bestaat niet");
                 new File(filename);
                }
    
                try
                {
                    BufferedWriter out = new BufferedWriter(new FileWriter(filename));
                    out.write(text);
                    out.close(); 
                }
    
                catch (IOException e)
                {
                    jOptionPane1.showMessageDialog(null,"Fout bij opslaan van bestand.");
                }
            }
        }
    Any ideas why it won't save my textfile in the current directory?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,563
    Blog Entries
    7
    Rep Power
    21

    Default

    You should have printed the entire stack trace by doing e.printStackTrace() in your catch clause; it'll most likely tell you what is wrong with your code.

    kind regards,

    Jos

  3. #3
    dejos456 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default

    I just started to work with java so i'm not that skilled

    i did:
    catch (IOException e)
    {
    e.printStackTrace();
    jOptionPane1.showMessageDialog(null,e);

    }

    when i save in current directory or any other directory of my current directory it says:
    java.io.FileNotFoundException: c:\haha.txt (Acces denied)

    And in the netbeans console i get alot of errors:
    Java Code:
    java.io.FileNotFoundException: C:\haha.txt (Toegang geweigerd)
            at java.io.FileOutputStream.open(Native Method)
            at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
            at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
            at java.io.FileWriter.<init>(FileWriter.java:46)
            at my.texteditorUI.texteditorUI.jMenuItem2ActionPerformed(texteditorUI.java:332)
            at my.texteditorUI.texteditorUI.access$100(texteditorUI.java:17)
            at my.texteditorUI.texteditorUI$2.actionPerformed(texteditorUI.java:111)
            at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
            at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
            at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
            at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
            at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
            at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1225)
            at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1266)
            at java.awt.Component.processMouseEvent(Component.java:6263)
            at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
            at java.awt.Component.processEvent(Component.java:6028)
            at java.awt.Container.processEvent(Container.java:2041)
            at java.awt.Component.dispatchEventImpl(Component.java:4630)
            at java.awt.Container.dispatchEventImpl(Container.java:2099)
            at java.awt.Component.dispatchEvent(Component.java:4460)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
            at java.awt.Container.dispatchEventImpl(Container.java:2085)
            at java.awt.Window.dispatchEventImpl(Window.java:2475)
            at java.awt.Component.dispatchEvent(Component.java:4460)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    When i save to other directory then c:\..... it works.
    any ideas?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,563
    Blog Entries
    7
    Rep Power
    21

    Default

    This is not a Java mistake at all; you are not running your program with root privileges, or "Administrator rights", (which is a wise thing) and you simply don't have the rights to write to your root directory.

    kind regards,

    Jos

  5. #5
    dejos456 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default

    Ok :D when i change 'working folder' to somewhere else it works like a charm ^^

    Thx for the replies

Similar Threads

  1. upload and save file
    By chennee72 in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 06-29-2011, 02:27 PM
  2. save file based on file extension
    By masa in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2010, 11:17 AM
  3. how to save file..
    By jont717 in forum New To Java
    Replies: 2
    Last Post: 02-12-2009, 11:33 PM
  4. File dialog with save
    By manpreet in forum New To Java
    Replies: 1
    Last Post: 01-02-2009, 05:44 PM
  5. [SOLVED] Save an xml file
    By mvg in forum XML
    Replies: 8
    Last Post: 10-13-2008, 12:25 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
  •