Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create file in resources/source folder in java programmatically?

I have two resources folders.

src - here are my .java files

resources - here are my resources files (images, .properties) organized in folders (packages).

Is there a way to programmatically add another .properties file in that resources folder?

I tried something like this:

public static void savePropertiesToFile(Properties properties, File propertiesFile) throws IOException {
        FileOutputStream out = new FileOutputStream(propertiesFile);
        properties.store(out, null);
        out.close();
    }

and before that created:

new File("/folderInResources/newProperties.properties");

But it looks for that path on the file system. How can I force it to look in the resources folder?

EDIT: Let me say what is it about. I have a GUI application and I support 2 languages (2 .properties files in resources folder). Now I added a option that user can easily translate application and when he finishes I save that new .properties on a disk in some hidden folder and read it from there. But I was hoping I could save that new .properties files (new language) next to the current languages (resources folder). I have a static Messages class which knows how to load resources both from the disk and both the default ones in resources folder. But if user takes this .jar file on some other machine, he would't have that new languages since they are on disk on that computer, not inside .jar file.

like image 646
vale4674 Avatar asked Dec 15 '11 00:12

vale4674


People also ask

How do you create a resource file in Java?

Java creating file with FileThe File's createNewFile method creates a new, empty file named by the pathname if a file with this name does not yet exist. The createNewFile returns true if the named file does not exist and was successfully created; false if the named file already exists.

How do you load properties file from resources folder in Java?

In Java, we can use getResourceAsStream or getResource to read a file or multiple files from a resources folder or root of the classpath. The getResourceAsStream method returns an InputStream . // the stream holding the file content InputStream is = getClass(). getClassLoader().

How do I create a resource folder?

Add a resource directory If you need to add a new resource directory, follow these steps: Click the target app module in the Project window, and then select File > New > Android resource directory.

How do I create a directory and file in Java?

In Java, we can use the File object to create a new folder or directory. The File class of Java provide a way through which we can make or create a directory or folder. We use the mkdir() method of the File class to create a new folder.


2 Answers

Java 8 Solution

 Path source = Paths.get(this.getClass().getResource("/").getPath());
        Path newFolder = Paths.get(source.toAbsolutePath() + "/newFolder/");
        Files.createDirectories(newFolder);

This will surely create new folder in resource folder. but you will find new folder in your target runtime.

which will be ProjectName/target/test-classes/newFolder. if you are running this code in test case. Other wise it would be in target/classes

Don't try to find new folder in your src/resources. it will be surely in target/test-classes or target/classes.

like image 140
Rohit Kumar Avatar answered Sep 19 '22 11:09

Rohit Kumar


As other people have mentioned, resources are obtained through a ClassLoader. What the two current responses have failed to stress, however, is these points:

  • ClassLoaders are meant to abstract the process of obtaining classes and other resources. A resource does not have to be a file in a filesystem; it can be a remote URL, or anything at all that you or somebody else might implement by extending java.lang.ClassLoader.
  • ClassLoaders exist in a child/parent delegation chain. The normal behavior for a ClassLoader is to first attempt to obtain the resource from the parent, and only then search its own resources—but some classloaders do the opposite order (e.g., in servlet containers). In any case, you'd need to identify which classloader's place for getting stuff you'd want to put stuff into, and even then another classloader above or below it might "steal" your client code's resource requests.
  • As Lionel Port points out, even a single ClassLoader may have multiple locations from which it loads stuff.
  • ClassLoaders are used to, well, load classes. If your program can write files to a location where classes are loaded from, this can easily become a security risk, because it might be possible for a user to inject code into your running application.

Short version: don't do it. Write a more abstract interface for the concept of "repository of resource-like stuff that I can get stuff from," and subinterface for "repository of resource-like stuff that I can get stuff from, but also add stuff from." Implement the latter in a way that both uses ClassLoader.getContextClassLoader().getResource() (to search the classpath) and, if that fails, uses some other mechanism to get stuff that the program may have added from some location.

like image 33
Luis Casillas Avatar answered Sep 19 '22 11:09

Luis Casillas