Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-28-2009, 01:03 PM
Member
 
Join Date: Nov 2009
Posts: 18
Rep Power: 0
dejos456 is on a distinguished road
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:

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?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-28-2009, 01:28 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,364
Rep Power: 5
JosAH is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-28-2009, 02:14 PM
Member
 
Join Date: Nov 2009
Posts: 18
Rep Power: 0
dejos456 is on a distinguished road
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:
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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-28-2009, 02:56 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,364
Rep Power: 5
JosAH is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-28-2009, 03:13 PM
Member
 
Join Date: Nov 2009
Posts: 18
Rep Power: 0
dejos456 is on a distinguished road
Default
Ok when i change 'working folder' to somewhere else it works like a charm ^^

Thx for the replies
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
save file based on file extension masa AWT / Swing 4 05-11-2010 11:17 AM
how to save file.. jont717 New To Java 2 02-12-2009 11:33 PM
File dialog with save manpreet New To Java 1 01-02-2009 05:44 PM
upload and save file chennee72 JavaServer Pages (JSP) and JSTL 2 11-21-2008 06:35 AM
[SOLVED] Save an xml file mvg XML 8 10-13-2008 12:25 PM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 08:27 AM.



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