Read file located in another Package

Asked

Viewed 53 times

2

inserir a descrição da imagem aqui

I have 3 Packages, one where the Gui class is located, another where the data reading "scripts" (in main) and photos and text files in the Resources.

    File file = new File("/com/Convocatoria/resources/DB.txt");
    Scanner sc = new Scanner(file);

I will use the scanner to read the file however it gives me the error

Unhandled Exception type Filenotfoundexception

  • This hierarchy you are using does not exist in the image. That’s why the error.

  • @Articuno What do you mean? There are the Sources and the Convocatoria

  • There is no packace "with".

  • @Article It is not necessary to say before the "project name"?

  • If you don’t have any package with in the project, why would you need to quote something that doesn’t exist?

  • with, it would only be necessary if it were a package... the fact that it always exists as a package is because Oracle recommended the organization of the packages with the site name in reverse... www.google.com the packages would be br/com/google

  • @Okay, thank you very much!

  • @Andrémoreira only one addendum, Voce has not only 3 Packages, Voce has actually 4, Convocatoria is also a package that holds the other 3. Oh, if the answer helped you, you can accept it by clicking on v the left so the answer serves as reference as a solution for others who find it in search.

Show 3 more comments

1 answer

3

The error is caused by the hierarchy of packages given as the path in the class. By print, there is no "com" package or any package with this in the name, therefore, this path entered is non-existent in the project.

You must inform the path to the archive, always following its inheritance of packages, in case, only /Convocatoria/resources/DB.txt would be enough:

File file = new File("/Convocatoria/resources/DB.txt");
Scanner sc = new Scanner(file);

There is no obligation to inform with in java, this is just a convention you inherited from android, to organize the hierarchy, but if you don’t follow, you don’t need to use it when passing the path of some file.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.