Results 1 to 5 of 5
Thread: Save file
- 11-28-2009, 01:03 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 18
- Rep Power
- 0
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:
Any ideas why it won't save my textfile in the current directory?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."); } } }
- 11-28-2009, 01:28 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,589
- Blog Entries
- 7
- Rep Power
- 17
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
- 11-28-2009, 02:14 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 18
- Rep Power
- 0
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:
When i save to other directory then c:\..... it works.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)
any ideas?
- 11-28-2009, 02:56 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,589
- Blog Entries
- 7
- Rep Power
- 17
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
- 11-28-2009, 03:13 PM #5
Member
- Join Date
- Nov 2009
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
upload and save file
By chennee72 in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 06-29-2011, 02:27 PM -
save file based on file extension
By masa in forum AWT / SwingReplies: 4Last Post: 05-11-2010, 11:17 AM -
how to save file..
By jont717 in forum New To JavaReplies: 2Last Post: 02-12-2009, 11:33 PM -
File dialog with save
By manpreet in forum New To JavaReplies: 1Last Post: 01-02-2009, 05:44 PM -
[SOLVED] Save an xml file
By mvg in forum XMLReplies: 8Last Post: 10-13-2008, 12:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks