Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a folder in eclipse for storing serialized objects?

I want to serialize some objects in my Java code. I don't want to put it in some random folder on the hard drive. I want it to be inside A FOLDER in my eclipse project folder. How do I make this folder and store my objects in it ?

Is this a good practice ? Will there be a problem if I try to make a self-contained JAR out of this project ?

like image 406
david blaine Avatar asked Mar 29 '13 10:03

david blaine


People also ask

How to hide the attributes of reference info objects in Eclipse?

Not available in Eclipse.Can be viewed in SAP GUI. We can hide the attributes of reference info objects in Attributes tab using a property called Visible to Consumers. In SAP BW 7.5 (Edition for SAP HANA), two additional tabs for maintaining Extended and Runtime Properties are introduced in the info object design/maintenance screen.

How do I create a Java project in Eclipse?

You can create an Eclipse project and add references to the directory containing the source files. Select Window > Show View > Other > Java > Package Explorer. Right-click inside the Package Explorer view and select New > Project. Enter a name for the new project and click Finish.

Should we serialize production data in a database?

Future requirements (that we don’t know yet) might be hard to achieve if we’ve some production data serialized in a database. It might be a valuable option tough for data storage tables when the client only reads the content and performance is not of great importance (like reporting).

How to create a source folder in Visual Studio Code?

Now, create a source folder. First press the right mouse button and go to “New” and “Source Folder”. 2. Give the source folder a name in our case “SF”. And press “finish”. The next part is to create a package.


5 Answers

Below source code shows how to create subfolder in current directory:

import java.io.File;

public class SourceCodeProgram {

    public static void main(String argv[]) throws Exception {
        File currentFolder = new File(".");
        File workingFolder = new File(currentFolder, "serialized");
        if (!workingFolder.exists()) {
            workingFolder.mkdir();
        }
        System.out.println(workingFolder.getAbsolutePath());
    }
}

When you run this source code and refresh Eclipse project you should see serialized directory in Eclipse project structure. Now you can use it and store all files in it.

In my app, when I want to store some data in hard drive, I always use user recommendation. I create application.properties file which contains key:

working.folder=/example/app-name

App reads this file at the start and creates this folder if it needs it. Second solution can be "app parameter". When I need only one or two parameters from users I read it from command line. For example user run my app using that command:

java -jar app.jar -dir /example/app-name

When user do not provide any folder I use default folder - current directory.

helpful links:

  • How to get Current Directory through File.
  • Getting the Current Working Directory in Java.
  • List item.
  • args4j.
  • Reading Properties file in Java
like image 195
Michał Ziober Avatar answered Nov 15 '22 00:11

Michał Ziober


It depends what information those serializable objects have.

Also if you want to have a folder inside your codebase (but deployed as a folder only), you can write code, to write or read files:

URL dir_url = ClassLoader.getSystemResource(dir_path);
// Turn the resource into a File object
File dir = new File(dir_url.toURI());
// List the directory
String files = dir.list()

Note directory should be in classpath.

like image 31
Himanshu Bhardwaj Avatar answered Nov 15 '22 00:11

Himanshu Bhardwaj


Steps:

  1. Right click your project's folder in the Package Explorer
  2. Locate the "New" menu item and hover it
  3. Locate "Folder" in the opened menu and click it
  4. Give your folder a name
  5. Click the "Finish" button at the bottom

This will create a folder that should be accessible from your working directory when running from within eclipse.

It is a bad idea to fiddle with files that are within the JAR file during runtime. Imagine a scenario when there are two or more JVMs running the same JAR, this will also mean they work with the same files and reading/writing to those files may cause collisions. You would generally want to separate those files from each other.

So unless those files are read-only, you should not include them in your JAR (otherwise it is fine)

like image 27
Ben Barkay Avatar answered Nov 15 '22 01:11

Ben Barkay


Aren't you better off writing to the user directory, e.g. in a subfolder like .myApp/?

You would do something like this to set up/initialize the directory:

File userDir = new File(System.getProperty("user.home"));
File storageDir = new File(userDir, ".myApp");
if (!storageDir.exists())
    storageDir.mkdir();
// store your file in storageDir

Have a look here to find out where userDir is usually located (though you don't necessarily need to care).

like image 42
skirsch Avatar answered Nov 15 '22 00:11

skirsch


If you launch your program in Eclipse, then by default the current working directory of the process will be the project's root directory. Then you can initialize and use a directory named serialized in your project's root like this:

File serializedDir = new File("serialized");
if (!serializedDir.exists()) {
    serializedDir.mkdir();
}

Relative paths in your code will be relative the working directory, which is the project's root by default. You can change the default working directory in the launcher configuration.

It depends on your project whether this is a good solution or not. If you don't want/need to share the serialized objects with others then I see nothing wrong with this.

When using the Export function of Eclipse to create a jar from the project, keep in mind that the folder will be selected by default. You have to explicitly deselect it to exclude from the jar. (In any case, it's better to use Maven or Ant for generating jars instead of Eclipse, which you only have to configure once, so no need to worry of the directory getting included by accident.)

You probably also want to exclude the directory from version control.

like image 24
janos Avatar answered Nov 15 '22 01:11

janos