java - Using the FileWriter with a full path -


i specified full path of file location when created filewriter, did not see file being created. did not error during file creation.

here's snippet of code:

public void writetofile(string fullpath, string contents) {     file file = new file(fullpath, "contents.txt");     if (!file.exists()) {         try {             file.createnewfile();         } catch (ioexception e) {             e.printstacktrace();         }     }     try {         bufferedwriter bw = new bufferedwriter(new filewriter(file.getabsolutefile()));         bw.write(contents);         bw.close();     } catch (ioexception e) {         e.printstacktrace();     } } 

fullpath "d:/codes/sources/logs/../../bin/logs". have searched whole directory, cannot find file anywhere. if specify filename [file file = new file("contents.txt");] , able save contents of file, not placed on preferred location.

how can save file content preferred location?

update: printed full path using file.getabsolutepath(), , getting correct directory path. [d:\codes\sources\logs....\bin\logs\contents.txt] when file in directory, cannot find there.

make sure add trailing backslashes path parameter path recognized directory. example provide windows os uses backslashes escaped. more robust method use file.separator property system.

works

writetofile("d:\\documents , settings\\me\\desktop\\development\\",                 "this test"); 

doesn't work

writetofile("d:\\documents , settings\\me\\desktop\\development",                 "this test"); 

file separator example

string fs = system.getproperty("file.separator"); string path = fs + "documents , settings" + fs + "me" + fs         + "desktop" + fs + "development" + fs; writetofile(path, "this test"); 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -