3

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

If I use C:\Users\Anny\Dropbox\SocialMediaOcto\instructions\Trees\instructions.txt as my absolute path for the file, IDEA cannot read or execute from this path. If I take that same path and paste it into windows explorer, it will execute right away. I dont want to focus on a working directory as this file works as the program's configurations file, but replaceing the slashes with backslashes doesnt work, the absolute path still brings me to the file, but IDEA doesnt launch.

I'm at 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, but can launch file on windows explorer..
    System.out.println("FILE EXIST AND 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 with forward slashes, the reader still cannot properly read or execute the file.

LOG: The 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 the file specified)
Jayizzle
  • 532
  • 1
  • 6
  • 24

1 Answers1

6

Correct url:

String instructions_file = "C:/Users/Anny/Dropbox/SocialMediaOcto/instructions/Trees/instructions.txt";

Because \ is an escape character in Java. If you want to use \ as a character you have to escape it itself.

Correct Url v2:

String instructions_file  = "C:\\Users\\Anny\\Dropbox\\SocialMediaOcto\\instructions\\Trees\\instructions.txt";

What you had:

String instructions_file  = "C:\Users\Anny\Dropbox\SocialMediaOcto\instructions\Trees\instructions.txt";

is read by java as

"C:{something}sers{something}nny{something}ropbox{something}ocialMediaOcto{something}nstructions\Trees\instructions.txt"

In my oppinion it's much better to use the first approach as it's platform safe.

xenteros
  • 15,586
  • 12
  • 56
  • 91
  • I think I was building it with \\ as well, but I'll work on this. Thanks. – Jayizzle Sep 02 '16 at 07:16
  • 1
    @Jayizzle using forward slash is much better approach! it's platform safe. Using backslash might not work on some operating system. If you find this answer helpful please mark it as a solution by clicking the gray tick under the score. – xenteros Sep 02 '16 at 07:16
  • Wait, no, it didn't quite work. Its still throwing the same error. – Jayizzle Sep 02 '16 at 07:20
  • I have no backward slashes in any of my strings or program arguments, and its throwing the same errors. – Jayizzle Sep 02 '16 at 07:25
  • Check your path again or give more information. – xenteros Sep 02 '16 at 07:29
  • I added more to the main via an edit, I basically just replaced my old path with your correct URL v1, and it throws the same error. – Jayizzle Sep 02 '16 at 07:30
  • Maybe there is a spelling error. You might have double extension if working on windows. Make sure you have "show known file extensions" on – xenteros Sep 02 '16 at 07:31
  • I dont think there is a spelling error if I can copy/paste the path into explorer and it takes me to the file. I am working on windows however. – Jayizzle Sep 02 '16 at 07:32
  • [Display all file names in that directory](http://stackoverflow.com/questions/1844688/read-all-files-in-a-folder) – xenteros Sep 02 '16 at 07:34
  • @Jayizzle is it correct that you get "Bees" logged instead of "Trees"? – xenteros Sep 02 '16 at 07:37
  • when crawling them, I got filename.txt.txt. I built the path like so: String instructions_file = instructions + folder +"/"+"instructions.txt"; – Jayizzle Sep 02 '16 at 07:39
  • That's exactly what I thought. – xenteros Sep 02 '16 at 07:42