-2

The task is to create a new file in the existing resource folder (named "local", for example)

  File file1 = new File("src/main/resources/" + "local" + "/" + "newFile.zip");
  file1.createNewFile();

But nothing is created. And no exception is thrown.

I tried also create, by using absolute path.

 File file1 = new File(File("src/main/resources/").getAbsolutePath() + "/" + "local" + "/" + "newFile.zip");
  file1.createNewFile();

But with the same result.

What am I doing wrong?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Jelly
  • 972
  • 1
  • 17
  • 40
  • what does file1.createNewFile(); return? – Stultuske Apr 14 '23 at 12:01
  • does this line actually compile? File file1 = new File(File("src/main/resources/").getAbsolutePath() + "/" + "local" + "/" + "newFile.zip"); – Stultuske Apr 14 '23 at 12:02
  • 3
    You cannot. There is no `src/main/resources` at runtime as that is the classpath and you cannot modify the classpath. So even if this would work in your IDE, it would fail in your actual application and start throwing exceptions. Use the tmp directory or a directory outside of the classpath instead. – M. Deinum Apr 14 '23 at 12:04
  • try to create complete folder structure and then try your code. – Sachin Kumar Apr 14 '23 at 12:07
  • Stultuske, it return false – Jelly Apr 14 '23 at 12:11
  • Sachin Kumar, I don't understand you clearly. Do you mean, I should create folder "local" in resources first, and then create files ? – Jelly Apr 14 '23 at 12:13
  • M.Deinum, you mean, I need to create new folder in the root of my project ? – Jelly Apr 14 '23 at 12:18
  • I guess getting the `new File("src/main/resources/") ` is not found, therefor, is useless to get absolute path from there. Try writing directly the full path, and if it works, try to get the current working dir, and carry on from there – Mayday Apr 14 '23 at 12:20
  • Why are you not using NIO `Files.createFile()` instead? – SquidXTV Apr 14 '23 at 12:23
  • 2
    As people have already correctly said, you shouldn't be using any paths which are particular to your project/IDE. When your app is properly packaged, you cannot know from where it will be run. You therefore need to use a directory off `java.io.tmpdir` or `user.home`. Moreover, resources are read-only, so why are you writing to a resource location at all? – g00se Apr 14 '23 at 13:40

1 Answers1

2

A program should never attempt to modify itself.

  • The src/main/resources directory is called “source” for a reason. Source code is compiled into a separate program. The program only contains the executable program (class files and data), not the source.
  • Java programs are distributed as .jar files (or jlink’d images made from .jar files). A .jar file is one file, specifically an archive file, and its individual entries are not files and are not writable.

In summary: when other people run your code, there is nothing you can modify.

The correct way to persist user data is to write it to a known location, usually somewhere under the user’s home directory.

It is perfectly acceptable to keep read-only default data bundled with your application, which gets used if the file in the user’s known location isn’t found.

VGR
  • 40,506
  • 4
  • 48
  • 63