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.
File file = new File("src/files/file.txt");orBufferedReader in = new BufferedReader(new FileReader( file.getPath()));- Alexey Shimansky