0

I have been searching for a way to get a file object from a file, in the resources folder. I have read a lot of similar questions on this website but non fix my problem exactly. Link already referred to how-to-get-a-path-to-a-resource-in-a-java-jar-file that got really close to answering my question:

String path = this.getClass().getClassLoader().getResource(<resourceFileName>)
.toExternalForm()

I am trying to have a resource file that I can write data into and then bring that file object to another part of my program, I know I can technically create a temp file that, I then write data into then pass it into a part of my program, the problem with this approach is that I think it can take a lot of system recourses, my program will need to create a lot of these temp files.

Is there any way, I can reuse one file in the resource folder? all I need is to get it's path (and it needs to work in a jar).I have tried this snipper of code i created for testing, i don't really know why it returns false, because in the ide it returns true.

    public File getFile(String fileName) throws FileNotFoundException {
        //Getting file from the resources folder
        ClassLoader classLoader = getClass().getClassLoader();
        URL fileUrl = classLoader.getResource(fileName);

        if (fileUrl == null)
            throw new FileNotFoundException("Cannot find file " + fileName);

        System.out.println("before: " + fileUrl.toExternalForm());

        final String result = fileUrl.toExternalForm()
                .replace("jar:" , "")
                .replace("file:" , "");
        

        System.out.println("after: " + result);

        return new File(result);
    }

Output:

before: jar:file:/C:/Users/%myuser%/Downloads/Untitlecd.jar!/Recording.wav
after: /C:/Users/%myuser%/Downloads/Untitlecd.jar!/Recording.wav
false
ishant kaushik
  • 891
  • 6
  • 18
  • 1
    "files" in a JAR are not actual files and cannot be represented by `File` URLs.In your IDE, the resources are actual files but in your JAR, the resources are part of the JAR and therefore not real files. – dan1st Nov 17 '21 at 23:15

1 Answers1

0

i have been searching for a way to get a file object from a file in the resources folder.

This is flat out impossible. The resources folder is going to end up jarred into your distribution, and you can't edit jar files, they are read only (or at least, you should consider them so. Non-idiotic deployments will generally mark their own code files (which includes those jars) as read-only to the running process. Even if not, editing jar files is extremely heavy and not something you want to do. Even if you do, on windows, open files can't be edited/replaced like this without significant headaches).

The 'resources' folder simply isn't designed for files that are meant to be modified.

The usual strategy is to make a directory someplace (for example, the user's home dir, accessing via System.getProperty("user.home"), and then make/edit files within that dir. If you wish, you can put templates in your resources folder and use those to 'initialize' that dir hanging off the user's home dir with a skeleton version.

If you have a few ten thousand files to make, whatever process needs this needs to be adjusted to not need this. For example, by using a database (H2, perhaps, if you want to ship it with your java app and have it be as low impact as possible).

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72