How to create a folder in eclipse to store serialized objects?

I want to serialize some objects in my Java code. I do not 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 to create this folder and save my objects in it?

Is this a good practice? Will there be a problem if I try to make a standalone JAR from this project?

+5
source share
7 answers

The source code below shows how to create a subfolder in the 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 update the Eclipse project, you should see a directory serializedin the structure of the Eclipse project. Now you can use it and store all files in it.

, , . application.properties, :

working.folder=/example/app-name

, . " ". , . , :

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

- , - .

:

+6

, .

, ( ), , :

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()

.

+3

:

  • "" .
  • "" .
  • ""

, eclipse.

, JAR . , JVM, JAR, , , / . .

, , JAR ( )

+3

, . , .myApp/?

- , / :

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

, , userDir ( ).

+2

Eclipse, . serialized :

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

, . .

, . / , .

Eclipse , . , . ( Maven Ant Eclipse, , , .)

, .

+2

: A) / B) .

A) , , , , : 1) , . 2) , . , , eclipse, eclipse.

B) FileOutputStream. . http://www.tutorialspoint.com/java/java_serialization.htm. ArrayList ( ), .

, , , , . , F5 eclipse, .

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;


public class CreateDirAndSerialize {


    public static void main(String args[])
    {
        ArrayList<String> sampleString = new ArrayList<String> ();
        sampleString.add("Test1");
        sampleString.add("Test2");
        sampleString.add("Test2");
        //Get the directory
        File directory = getSerializedDirectory();
        writeObjects(directory, sampleString);
    }

    public static void writeObjects(File directory, Object object)
    {
        try
          {
             FileOutputStream fileOut =
             new FileOutputStream(directory+"//serializedData");
             ObjectOutputStream out =
                                new ObjectOutputStream(fileOut);
             out.writeObject(object);
             out.close();
              fileOut.close();
          }catch(IOException i)
          {
              i.printStackTrace();
          }
    }

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

}

eclipse, eclipse. Eclipse, eclipse, , .

, , , , singleton . , .: - .

. / : http://www.xyzws.com/Javafaq/what-are-rules-of-serialization-in-java/208

+2

You can ignore Eclipse and create the directory (and the files in it) programmatically or manually.

Then update the project explorer view in Eclipse and voila, the directory and files are part of the view.

0
source

All Articles