Write to a file under program files directory in vista

Hi,

How can i write a file under program files directory in vista? It seems like that there is a permission problem. The following code does nothing and also i don't get any error messages.

Thanks in advance.

publicvoid test(){

try{

String myFile ="C:/Program Files/AnyApp/test.txt";

FileWriter fw =new FileWriter(myFile,true);

BufferedWriter writer =new BufferedWriter(fw);

for (int i = 0; i < 10; i++){

writer.append("test");

}

writer.flush();

writer.close();

}catch (Exception e){

e.printStackTrace();

}

}

[1181 byte] By [vharuna] at [2007-11-27 4:07:00]
# 1
not sure if u'll need to also close the FileWriter before u can see the test.txt?
JWKC-5ivea at 2007-7-12 9:12:10 > top of Java-index,Core,Core APIs...
# 2
Closing the BufferedWriter closes the FileWriter.
sabre150a at 2007-7-12 9:12:10 > top of Java-index,Core,Core APIs...
# 3

Your code should first create the directory "AnyApp" as follows:

File file = new File("C:/Program Files/AnyApp");

boolean created = file.mkdir();

if (created) {

// Then you can create the file in this directory like you've done in your test() method.

}

On Windows Vista the file will be created here:

C:\Users\John\AppData\Local\VirtualStore\Program Files\AnyApp\text.txt

Where "John" is the user who executed the above code.

Neodymiuma at 2007-7-12 9:12:10 > top of Java-index,Core,Core APIs...