SRL Archive - General

Saving a file to desktop in java


rj

2013-02-13 22:56:29

So I was messing around with file saving in java and came up with:

import java.io.*;

public class TestList {
public static void main(String[] args) throws IOException{
File f;
f=new File("C:\\testfilemake.doc");
//if(!f.exists()){
f.createNewFile();
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write("TROLOLOLOL");
output.close();
}
}
//}

And I get this error:
Exception in thread "main" java.io.IOException: Access is denied
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at TestList.main(TestList.java:9)

I'm guessing im not using this correctly:

		f=new File("C:\\testfilemake.doc");

How would I save this to desktop?


Btw using:

		f=new File("testfilemake.doc");

Just saves it to a random folder lol

i luffs yeww

2013-02-13 23:02:50

Run it as administrator.

rj

2013-02-13 23:03:34

I thought I was administrator on my own computer, and I want this to work on others peoples computer

i luffs yeww

2013-02-13 23:08:52

From: rjj95
I thought I was administrator on my own computer, and I want this to work on others peoples computer


Then don't save to the C drive.

Neodymium

2013-02-13 23:32:48

If you want to save to Desktop, you would use C:/Documents and Settings/User/Desktop/file or C:/Users/User/Desktop/file depending on your version.
Also, the second snippet is suppose to save to the same place the class is run from (unless it's saving to a temp folder since it no write access).

sm321

2013-02-21 13:28:11

I'm not sure if you've already sorted this, but another way around this is to make the file then set that as the save path. Unless you want to create a new file every time…