save and retrieve multiple of arraylists to a file - java

Hi i was wondering if this was possible. i have 5 multiple array lists. after i add information to each array lists is it possible to save this as a single file using filechooser. i have 5 arraylist which i have created as a object from a different class.
hotelArrayObjects.getDahabArray()
hotelArrayObjects.getRomeArray()
hotelArrayObjects.getParisArray()
hotelArrayObject.getBerlinArray()
hotelArrayObjects.getNiceArray()
wondering if there was any way to do this. I was thinking of using Filechooser to make it more user friendly and easier to retrieve.
Any tips on how to tackle this problem?

You can use java.io.ObjectOutputStream and its subclasses for writing objects to files. You can read them back with java.io.ObjectInputStream and its subclasses. There are readObject() and writeObject() methods in these classes that you can use. Note that if you want to write an object to a file, the defining class of that object should be serializable. In you case, objects that you store in the ArrayList should be serializable.
Java mechanisms for storing and retrieving objects does not need the file to be selected with a FileChooser. You can use java.io.File class for opening a stream to your file.

If you want to save the information within an arraylist, you can do a few forloops then use a file writer
String s1;
for(int i=0; i<arraylist.length(); i++) {
s1+=arraylist.get(i);
}
repeat repeat
then use something like this:
String finalString = s1+s2+s3+s4+s5;
FileWriter fwrite = new FileWriter("hotels.txt");
BufferedWriter out = new BufferedWriter(fwrite);
out.write(finalString);
Then you can use the JFileChooser to save that file.

Related

Store byte array and string in same file

Earlier I was storing only string in my file which can be store in SD card now I would to store byte[] also in same files. So do I just need to store normal to the file like this:
for string:- bufferWritter.write(data);
for bytes:- FileUtils.writeByteArrayToFile(new File("pathname"), myByteArray)
or fos.write(myByteArray);
So if I do like that then how can I differ message whether it is string or byte[].
Even I would like to know that is this is a good way to do?
You would need to write some sort of header to your file which should state what it does represent.
If you have multiple items in one file, also specify the length of the data parts.
[byte] sort of data (0=String, 1=Image)
And then the actual data.
But I would recommend you use a different format like json or make a serializable object.
I would give a try to some JSON implementation (maybe GSON? or some alternative) so you can have stored mixed data types in one file or write your own de/serialization routine so you can store whole objects.
Note: if you implement Serializable interface by a class that represent object to be stored, don't forget to re-generate UUID each time you change contents of that class, it will save you some time figuring out what went wrong

Java Object Array IO

I have an Array of Objects called "person" and i need to save that data, Is there a way to output an array and then input it back into the program. I have tried simply outputting the raw String and int data that makes up each "person" and the program never writes that data to the specified txt file. Is there a method or function that can write raw object data and then retrieve it?
You should have a look to ObjectOutputStream and ObjectOutputStream and the Serializable interface. What you are trying to do is to serialize your data to a file and load (deserialize) it back again.
You can use standard Java serialisation. Another trivial solution is to use XStream, which will serialise your objects into/from XML. You don't need to implement specific interfaces or annotate your objects (by default) and you'll get a readable (in XML terms) serialisation, which can be useful.
One approach would be to use Serialization provided by Java. You need to implement the java.io.Serializable interface and write and read to and from the ObjectOutputStream and ObjectInputStream classes to store and retrieve data respectively.
You need serialization. Read this.

convert byte[] to Object File in Java

What is the best way to convert a byte[] to Object java.io.File? i dont want write this in the disk just get a object File.
The File object is just a wrapper around the name for a real file on the disk. It does not contain any data. You will have to write your byte array into a file first. After the API you then call is done, you can maybe delete that file again. Search for how to manage temporary files in Java.
java has serilazebal interface to persistent your object;
i guess u want custom serialize method, like c strct;
refences :
https://github.com/MisterChangRay/magic-byte
http://code.google.com/p/javastruct/wiki/HowToUseJavaStruct

How to restrict raw data to save in text file

I am developing a small application in Java. At certain point i need to save the object of my custom class to a text file for this i override toString() method in my custom class and then use ObjectOutputStream class to save object of my custom class to text file. Now everything works fine i.e my text file contains the text but along with that text it also contians some strange characters or raw data aswell. Following three lines contain major code for that
ObjectOutputStream outputStream = null;
outputStream = new ObjectOutputStream(new FileOutputStream(filename));
outputStream.writeObject(person);//person is the instance of my custom class
Is there any way to restrict that raw data to be saved in my text file?
An ObjectOutputStream serializes objects that you pass to writeObject(). It doesn't write a text version of the object. If you want to write the value returned by toString() to a file, then open a file with something like a FileWriter, get the String by calling the toString() method, and then write it to the FileWriter.
Depending on your needs, you should should maybe have a look at the XML Serialization libraries such as JAXB or XStream.
The solution given above will only save the string representation of your object into a file (which could be what you want) but I guess that if you want to save an object into a file, it's probably to be able to access it afterwards.
Both JAXB and XStream provide methods to serialize/unserialize a Java object in XML. With XStream (maybe with JAXB also, I don't know), you can even serialize/unserialize your object(s) in JSON if you need something more compact.

Trying to make a program that imitates a Process Table

I am trying to create a program that is just like a Process Table. I have to implement a class PCB (Process Control Block) with several fields such as:process name (a string)
process priority (an integer)
register set values (an object of a class Register Set containing the following fields: XAR, XDI, XDO, PC.
Then my program needs to then create a Process Table data structure as either an array (max size 100 elements) or an arraylist of type PCB, and initialize the array with data from the file "processes1.txt" Then the Process Table arrraylist has to print out its contents by each process.
So my questions are:
1. How many programs/classes do I have to write? Is it 3. The first program that creates the Process Table arraylist of PCB. The 2nd class would be the PCB class that defines the PCB fields.
2. How would the first program initialize the arraylist with the data from the text file?
3. Could I use an ArrayList of an ArrayList? and how would I do that?
Thank you in advance.
ProcessTable, ProcessControlBlock, RegisterSet sound like good starts.
I'd create a method in ProcessTable called load(File file) that uses File, and perhaps TextReader to read the configuration. There are many ways to read a text file. Also google on BufferedInputStream. Examples abound.
ArrayLists can hold objects, and ArrayList is indeed an Object, so yes. The use is easy: someArrayList.add(someOtherArraylist); though the declaration is a little harder:
ArrayList<ArrayList<String>> a = new ArrayList<ArrayList<String>>();
which says 'a is to be an ArrayList containing other ArrayLists containing Strings.` there are other ways to write the declaration that are a little more general, but this shows the gist.

Categories

Resources