I started learning Java not so long ago, so it’s quite possible that my question is very, very stupid. I have a src package, inside which is another package - files.

  • The file package contains file.txt with random text,
  • The src package contains the class MainClass (inside which is the main method)

I want to read the text that is in file.txt using MainClass and output the text to the console. Google offered me different options, but they all did not find my file.txt! How can I do it? Thank you in advance!

  • Start reading a book by Horstman 1 volume. Here it is written in detail what and how to do. Already trashed question. - Vladimir Glinskikh
  • Thanks for the advice, and for the link. That as advised to do in the article - I get. But I need a file to find me without a full path. My file is located along this path: C: \ Users \ Evertum \ src \ files and if you specify it in full, it finds it. And it works. But I want to specify only files \ file.txt how to do this? and is it possible? - Evertum
  • Regarding the root of the project can not be specified? File file = new File("src/files/file.txt"); or BufferedReader in = new BufferedReader(new FileReader( file.getPath())); - Alexey Shimansky
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

As a rule, a J2EE application does not write files inside. The file is either outside the application, or the base is used instead of the file ....

For example, if you create a file, as in the example given by Vladimir in the comment

 public static void write(String fileName, String text) { //Определяем файл File file = new File(fileName); try { //проверяем, что если файл не существует то создаем его if(!file.exists()){ file.createNewFile(); } //PrintWriter обеспечит возможности записи в файл PrintWriter out = new PrintWriter(file.getAbsoluteFile()); try { //Записываем текст у файл out.print(text); } finally { //После чего мы должны закрыть файл //Иначе файл не запишется out.close(); } } catch(IOException e) { throw new RuntimeException(e); } } 

then calling the method and specifying the arguments as follows:

 FileWorker.write("myfile.txt", "my Test Text"); 

You will see that it will be created right outside the project src folder. More precisely straight in the same folder as src

Hence the recommendation: to make folders with resources and save outside the project. And to indicate the path already relative to them, i.e. in fact, the built project ( jar file for example) will be the root.

Or specify the absolute path.

For an unbuilt project, you can specify the path starting from the root, i.e.

 File file = new File("src/files/file.txt"); BufferedReader in = new BufferedReader(new FileReader( file.getPath())); и т.д. 

But make the most of it if it's just for yourself and for the tests. No more. Although I still do not recommend. It’s better to do it right away))

For it is necessary to know that after creation, for example jar - the src folder will not be. Therefore, there will be problems with this path)) Hence the recommendation, which was higher.