I am trying to save an image with this line

ImageIO.write(image, "JPG", new File(fileDestination.getText()));

but it is giving me an error.

java.io.FileNotFoundException: C:\Users\Adam (Access is denied)
	at java.io.RandomAccessFile.open(Native Method)
	at java.io.RandomAccessFile.<init>(Unknown Source)
	at javax.imageio.stream.FileImageOutputStream.<init>(Unknown Source)
	at com.sun.imageio.spi.FileImageOutputStreamSpi.createOutputStreamInstance(Unknown Source)
	at javax.imageio.ImageIO.createImageOutputStream(Unknown Source)
	at javax.imageio.ImageIO.write(Unknown Source)
	at screenCapture.CaptureScreen.actionPerformed(CaptureScreen.java:111)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$000(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$2.run(Unknown Source)
	at java.awt.EventQueue$2.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)

The user inputs the spot where they want to save the file to (using a JFileChooser).

I believe my error is that I do not have permission to write files to a computer. I have tried several ways of getting permission to write to the computer using SecurityManager.

could some one help me out?

Thanks

It most likely was, but it still doesn't show the directory that the user is currently in. Secondly I have never even seen or heard of that website before. They must have copied my post -_-

I have written to the file successfully before, I just need to fil in the directory that I am currently in.

My newest code looks like this. I tried implementing the security manager to get access to write to a directory. I just tried writing to the directory I tried to write to before, and it works now, so just ignore the security manager.

else if (e.getActionCommand().equals("Save Image")){
			name = nameField.getText();
			SecurityManager sm = System.getSecurityManager();
//			sm.checkPermission(new FilePermission (fileDestination.getText(), null));
			try {
				ImageIO.write(image, "JPG", new File((fileDestination.getText())+ "\\" + name + ".jpg"));
			} catch (IOException ex) {
				// TODO Auto-generated catch block
				ex.printStackTrace();
			}
		}

Now my question is why does it not display the current directory that the user is in?

why does it not display the current directory that the user is in

How is it supposed to display anything without a println statement? Can you explain?

For debugging purposes, build the full filename in a String and print it before using it in the write() method.

I figured it out. I had to get the directory, and then add in a backslash and then get the selected file. that part of the code now looks like this

String directory = fc.getCurrentDirectory().toString() + "\\" + fc.getName(fc.getSelectedFile());

Instead of using the backslashes, use the path separator variable defined in the File class.

This question has already been answered. Start a new discussion instead.