Java FileNotFoundException with Absolute path - Cant Read or Execute, but file exists -


this question has answer here:

i'm sure has been answered, ten different strategies hasn't worked on issue.

if use c:\users\anny\dropbox\socialmediaocto\instructions\trees\instructions.txt absolute path file, idea cannot read or execute path. if take same path , paste windows explorer, execute right away. dont want focus on working directory file works program's configurations file, replaceing slashes backslashes doesnt work, absolute path still brings me file, idea doesnt launch.

i'm @ wits end.

 public static string generatefilename(string folder){      string filename = "";     list<string> hashtags = new arraylist<>();     string instructions_file =         "c:\users\anny\dropbox\socialmediaocto\instructions\trees\instructions.txt";      //does not return true-true, can launch file on windows explorer..     system.out.println("file exist , execute?" + new file(instructions_file).getabsolutefile().canread() +" "+new file(instructions_file).getabsolutefile().canexecute());      system.out.println(new file(instructions_file).getabsolutefile());     //c:\users\anny\dropbox\socialmediaocto\instructions\trees\instructions.txt      bufferedreader br = null;      try {          string scurrentline;          br = new bufferedreader(new filereader(new file(instructions_file).getabsolutefile())); 

edit after replacing backslashes forward slashes, reader still cannot read or execute file.

log: string prints: c:/users/anny/dropbox/socialmediaocto/instructions/bees/instructions.txt

  java.io.filenotfoundexception:    c:\users\anny\dropbox\socialmediaocto\instructions\bees\instructions.txt (the system cannot find file specified) 

correct url:

string instructions_file = "c:/users/anny/dropbox/socialmediaocto/instructions/trees/instructions.txt"; 

because \ escape character in java. if want use \ character have escape itself.

correct url v2:

string instructions_file  = "c:\\users\\anny\\dropbox\\socialmediaocto\\instructions\\trees\\instructions.txt"; 

what had:

string instructions_file  = "c:\users\anny\dropbox\socialmediaocto\instructions\trees\instructions.txt"; 

is read java as

"c:{something}sers{something}nny{something}ropbox{something}ocialmediaocto{something}nstructions\trees\instructions.txt" 

in oppinion it's better use first approach it's platform safe.


Comments