I want to create a file in a directory in computers with Java -


i'm making game, , game requires directory made in appdata folder of user's account. problem is, can't know put it, every user different. on windows way. want know if should write special or...

file file = new file("c:\\users\\%user%\\appdata\\roaming\\[gamename]"); 

is there special name have give "%user%" (i used example), or there else gotta do?

you can use appdata environment variable "c:\users\username\appdata\roaming"

and can using system.getenv() function :

string appdata = system.getenv().get("appdata"); 

edit :

look @ example (create directory "mygame" , create file "mygamefile" directory). code awful it's give idea of how works.

string gamefolderpath, gamefilepath; gamefolderpath = system.getenv().get("appdata") + "\\mygame"; gamefilepath = gamefolderpath + "\\mygamefile";  file gamefolder = new file(gamefolderpath); if (!gamefolder.exists()) {     // folder doesn't exist. create     if (gamefolder.mkdir()) {         // folder created         file gamefile = new file(gamefilepath);         if (!gamefile.exists()) {             // file doesn't exists, create             try {                 if (gamefile.createnewfile()) {                     // mgamefile created in %appdata%\mygame !                 }                 else {                     // error                 }             } catch (ioexception ex) {                 // handle exceptions here             }         }         else {             // file exists         }     }     else {         // error     } } else {     // folder exists } 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

java - More than one row with the given identifier was found: 1, for class: com.model.Diagnosis -