In order to do this you need two things:
A) Access/Create the specific folder
B) Actually serialize your objects and save them to disk.
For A) this is certainly answered by the other answers here which show how to:
1) Check if folder exists.
2) Create the folder if it does not exist.
Additionally, projects launched in eclipse have as the working directory the eclipse project folder.
For B) you need to serialize your objects using the FileOutputStream .
See http://www.tutorialspoint.com/java/java_serialization.htm .
You can either serialize each object into a separate file, or create one class with an ArrayList (or some other data structure) that contains references to all the objects.
Below a sample Class doing just what you asked using Static Methods as I didn't want to have to instantiate an object. Also, you need to press F5 in your eclipse project to refresh the package explorer and view the new folder and files.
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;
}
}
As the question referred to projects within eclipse, the above is for code within an eclipse project. If you want to interact with Eclipse itself, we are talking about eclipse plugin development which is another story entirely so you need to specify that.
Finally, you can also create a custom class holding any variables you want and instantiate a singleton object for containing all your other objects. There are however some limitations when serializing objects, e.g. :
- Object references with static modifier are not serializable.
See this for some rules/tips for some serializable: http://www.xyzws.com/Javafaq/what-are-rules-of-serialization-in-java/208