• Post Reply Bookmark Topic Watch Topic
  • New Topic

A possibly realy stupid error (file not found although it is there)

 
Marcus Rauchfuss
Ranch Hand
Posts: 51
1
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I am writing an RPG character generator and the "Save file" does not work, I always get a "file not found error" although the file stuff is written to exists and I have added a line to create a file if it does not exist.
All to no avail.
This is the code:

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35716
412
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
createNewFile() returns false if it is fails. That's probably what's happening here.

You are using a path of:
/gui/testfile.txt

This means a folder named "gui" at the root of the file system. You probably want:
gui/testfile.txt

This means a folder named gui in the current directory. You do have a folder named gui, right?
 
Marcus Rauchfuss
Ranch Hand
Posts: 51
1
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. Does not help either, and yes, I have a gui package.
Now Eclipse tells me it cannot find the path.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35716
412
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To troubleshoot, put this in your code. What does it print out? Is it where your actual gui directory is? If not, how are they different?

 
Marcus Rauchfuss
Ranch Hand
Posts: 51
1
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I got the absolute path, but it does not work either.

I tried:

File file = new File("C:\\Users\\Marcus\\workspace\\CharacterGenerator\\gui\\testfile.txt");

and


File file = new File("C:/Users/Marcus/workspace/CharacterGenerator/gui/testfile.txt");



Still, the system cannot find the path.
 
Marcus Rauchfuss
Ranch Hand
Posts: 51
1
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK; I exported the program and it works fine, no idea, why Eclipse has a problm with it.
 
Ivan Leon
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't know about the Eclipse but, I went through the same problem, and one thing that I saw here during my searches for the answer, is that, in some cases, you have to throw an Exception in your method, or put a try/catch around your if/else, and here is the reason (taken from StackOverflow):

"Unchecked exceptions are those that extend RuntimeException class. Compiler will never force you to catch such exception or force you to declare it in the method using throws keyword. All other exception types (that do not extend RuntimeException) are checked and therefore must be declared to be thrown and/or catched."

The rule of thumb here is: for any exception (except RuntimeException), you must consider the usage of "throws Exception" or "try/catch". It depends on you.

Without a "try/catch" or "throw Exception", this could happen at Eclipse, for example:

"Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unhandled exception type IOException
at FileReader.main(FileReader.java:10)"


Considering that the possible exception that can occur from trying to create a file is "IOException", the rule above applies to this scenario

Here is a code which I used "throw Excepetion":



Regards
Ivan Leon
@ivanleoncz
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Leon wrote:. . .
"Unchecked exceptions are those that extend RuntimeException class. . . . All other exception types (that do not extend RuntimeException) are checked . . ." . . .
[pedantic mode]
An exception might be an Error and Errors are also unchecked exceptions. If you look in the Java® Language Specification (=JLS) §11.1, exception means objects which are instances of Throwable and instances of Exception are called Exceptions.
Exception and Throwable are themselves checked.
[/pedantic mode]
That JLS section tells you which exceptions are checked and unchecked.
That same SO link wrote:It is because,

Unchecked Exceptions are not a result of programmer's fault. Instead they are the serious consequences where we(programmer) aren't expected to do much with it.
In case of Checked Exception, it is an exception generated because of the programmer's fault & often can be resolved by programmer itself.
…but several people on that SO thread pointed out that quote is the wrong way round and incorrect.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!