• Annoying problem in Java
    7 replies, posted
I'm trying to read from files in Java, but apparently it fails at detecting if files exist. Here's a piece of code from it. [code]InputStream input = new FileInputStream("C:/avatar.png")[/code] It keeps throwing FileNotFoundExceptions and it annoys me because I'm 100% sure that file exists. What do I do now?!
[QUOTE=Dlaor;20686434]I'm trying to read from files in Java, but apparently it fails at detecting if files exist. Here's a piece of code from it. [code]InputStream input = new FileInputStream("C:/avatar.png")[/code] It keeps throwing FileNotFoundExceptions and it annoys me because I'm 100% sure that file exists. What do I do now?![/QUOTE] Try "C:\\avatar.png" just for kicks. Output what the FileNotFoundException says, it might have something useful.
[QUOTE=nullsquared;20686526]Try "C:\\avatar.png" just for kicks. Output what the FileNotFoundException says, it might have something useful.[/QUOTE] [code] System.out.println("Hello World!"); InputStream input; String a = "No error!"; try { input = new FileInputStream("C:\\avatar.png"); } catch (FileNotFoundException e) { e.printStackTrace(); a = "!!!" + e.getMessage() + "!!!"; } System.out.println(a);[/code] Am I doing it wrong? It outputs: Hello World! No error!
Seems like it's not throwing any exceptions then? Looks fine.
[QUOTE=nullsquared;20686526]Try "C:\\avatar.png" just for kicks. Output what the FileNotFoundException says, it might have something useful.[/QUOTE] Unix style /avatar.png works too.
Yeah, you just have to escape the string if you're going to use back slashes.
Hmm, weird language this is :v: It seemed it only threw the exception the first time it loaded the file or something... Thanks for the help!
[QUOTE=Dlaor;20687637]Hmm, weird language this is :v: It seemed it only threw the exception the first time it loaded the file or something... Thanks for the help![/QUOTE] If you use a IDE such as Netbeans or Eclipse it may take a sec before the file is compiled which results in the old code being used. After you saved the file just wait for the progressbar to finish (takes like 2 secs) to make sure you're running the new code.
Sorry, you need to Log In to post a reply to this thread.