Append object existing file - java

I would like to append Serializable objects in a existing file, but apparently it deletes the previous stored objects.
For the parameter File F, I send new File(file's path). Maybe my mistake is from there ?
Thank you for helping.
public static void wrinting(File[] tab, File f) throws Exception{
ArrayList<ImageClass> obj = imagesArray(tab);
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
for (ImageClass i : obj) {
oos.writeObject(i);
}
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}

Open the FileOutputStream like this instead:
new FileOutputStream(f, true)

Related

Can't create file in directory - Java

I want to save all elements of a HashMap in a file. To do this I wrote following code with the help of some google searches:
public void saveCalendars() {
try {FileOutputStream fos = new FileOutputStream(CALENDARPATH_STRING);
ObjectOutputStream oos = new ObjectOutputStream(fos);
for(Calendar elementCalendar : calendarRegister.values()) {
oos.writeObject(elementCalendar);
}
oos.close();
fos.close();
} catch (FileNotFoundException e) {
System.out.println("File not found");
try {FileOutputStream fos = new FileOutputStream(new File(CALENDARPATH_STRING));
ObjectOutputStream oos = new ObjectOutputStream(fos);
for(Calendar elementCalendar : calendarRegister.values()) {
oos.writeObject(elementCalendar);
}
oos.close();
fos.close();
} catch (IOException ex) {
System.out.println("Creating: Error initializing stream");
}
} catch (IOException e) {
System.out.println("Save: Error initializing stream");
}
}
With final static String CALENDARPATH_STRING = "C:\\Windows\\calendars.dat";.
I thought that I simply could use the same Code but with FileOutputStream fos = new FileOutputStream(new File(CALENDARPATH_STRING)); if the file hasn't been created yet to create one.
Unfortunately, it doesn't work. It's the firs time, that a make such saving stuff, so maybe you can help me.
A couple of suggestions:
Use File.createNewFile to create a new file and verify it's result
Use try-with-resources when dealing with IO stuff (I assume you use > JDK 7). You can read more about this feature on official site.
You can avoid duplications:
File calendarFile = new File(CALENDARPATH_STRING);
try {
if(calendarFile.createNewFile()) {
System.out.println("File not found. New file was created");
}
} catch (IOException e) {
System.out.printf("Can not create file %s\n", CALENDARPATH_STRING);
}
try(FileOutputStream fos = new FileOutputStream(calendarFile);
ObjectOutputStream oos = new ObjectOutputStream(fos)) {
for(Calendar elementCalendar : calendarRegister.values()) {
oos.writeObject(elementCalendar);
}
} catch (IOException e) {
System.out.println("Save: Error initializing stream");
}
Well, I can see that there is an issue with the path of your file CALENDAR_PATH_STRING
and use new File(CALENDAR_PATH_STRING) it would create a new file if the particular file was not found. Also in local I can see it is working.
public void saveCalendars(Map<String, Calendar> calendarRegister) {
try (FileOutputStream fos = new FileOutputStream(new File(CALENDAR_PATH_STRING));
ObjectOutputStream oos = new ObjectOutputStream(fos)) {
for (Calendar elementCalendar : calendarRegister.values()) {
oos.writeObject(elementCalendar);
}
} catch (IOException e) {
System.out.println("Creating: Error initializing stream");
}
}

How to avoid java.io.StreamCorruptedException?

I have a method that writes data from a list to a file, a method that reads data from a file into a list and a method that writes data from a list in a file to the specified number of times. I'm trying to extract data from a file after I use the first method writeFile () everything works fine. I read the data from the file into the list by readFile () method. After that I use my method which writes to the file the number of times I need, everything is fine, it writes multyWrite (). But after that I can not read the data from the file in the readFile () method since I get `
Exception stack trace:
Exception in thread "main" java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1599)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:431)
at ProductService.readFile(ProductService.java:47)
at Main.main(Main.java:21)
I know that I should use objectOutputStream.reset (), but where would it be better to use it?
private String fileName;
private ProductInterface<FlyingMachine> productService = new ProductInterfaceImpl();
private ObjectOutputStream objectOutputStream;
private FileOutputStream fileOutputStream;
public ProductService(String fileName) throws IOException {
this.fileName = fileName;
fileOutputStream = new FileOutputStream(fileName);
this.objectOutputStream = new ObjectOutputStream(fileOutputStream);
}
public void writeFile() throws IOException {
try {
for (FlyingMachine f : productService.getProductContainer()) {
objectOutputStream.writeObject(f);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (objectOutputStream != null) {
objectOutputStream.flush();
objectOutputStream.close();
fileOutputStream.close();
}
}
}`
public void readFile() throws IOException {
ObjectInputStream objectInputStream = null;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(fileName);
objectInputStream = new ObjectInputStream(fileInputStream);
while (fileInputStream.available() > 0) {
FlyingMachine flyingMachine = (FlyingMachine) objectInputStream.readObject();
productService.getProductContainer().add(flyingMachine);
}
} catch (ClassNotFoundException | EOFException e) {
e.printStackTrace();
} finally {
if (objectInputStream != null) {
objectInputStream.close();
fileInputStream.close();
}
}
}
public void multyWrite(int number) throws IOException {
for (int i = 0; i < number; i++) {
try {
fileOutputStream = new FileOutputStream(fileName, true);
objectOutputStream = new ObjectOutputStream(fileOutputStream);
for (FlyingMachine f : productService.getProductContainer()) {
objectOutputStream.writeObject(f);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (objectOutputStream != null) {
objectOutputStream.flush();
objectOutputStream.close();
}
}
}
}
You create a new ObjectOutputStream in the constructor. In writeFile you use that OOS instance and close it. But in multyWrite you don't use it and instead create new instances.
Now when you call multyWrite without having called writeFile first, that first OOS will still be open, but the OOS you create in multyWrite doesn't know that - thus causing your file to have two OOS headers after another.
And then when you try to read such a file, the ObjectInputStream will find the first header (all is fine) and then unexpectedly find the second header, while it expected a type code. That header starts with 0xAC, hence throwing the exception message "invalid type code: AC".
To fix this, either have multyWrite use the OOS constructed in your constructor, the same way writeFile does, or make sure that that OOS is closed before you create a new one.
It's generally not a good idea to open a stream (of any kind) in a constructor and then rely on external code calling a specific method to close it. Better create streams when you need them and close them directly.

Java Trouble outputting ArrayList to existing file [duplicate]

I am writing a program in Java which displays a range of afterschool clubs (E.G. Football, Hockey - entered by user). The clubs are added into the following ArrayList:
private ArrayList<Club> clubs = new ArrayList<Club>();
By the followng Method:
public void addClub(String clubName) {
Club club = findClub(clubName);
if (club == null)
clubs.add(new Club(clubName));
}
'Club' is a class with a constructor - name:
public class Club {
private String name;
public Club(String name) {
this.name = name;
}
//There are more methods in my program but don't affect my query..
}
My program is working - it lets me add a new Club Object into my arraylist, i can view the arraylist, and i can delete any that i want etc.
However, I now want to save that arrayList (clubs) to a file, and then i want to be able to load the file up later and the same arraylist is there again.
I have two methods for this (see below), and have been trying to get it working but havent had anyluck, any help or advice would be appreciated.
Save Method (fileName is chosen by user)
public void save(String fileName) throws FileNotFoundException {
String tmp = clubs.toString();
PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
pw.write(tmp);
pw.close();
}
Load method (Current code wont run - File is a string but needs to be Club?
public void load(String fileName) throws FileNotFoundException {
FileInputStream fileIn = new FileInputStream(fileName);
Scanner scan = new Scanner(fileIn);
String loadedClubs = scan.next();
clubs.add(loadedClubs);
}
I am also using a GUI to run the application, and at the moment, i can click my Save button which then allows me to type a name and location and save it. The file appears and can be opened up in Notepad but displays as something like Club#c5d8jdj (for each Club in my list)
You should use Java's built in serialization mechanism.
To use it, you need to do the following:
Declare the Club class as implementing Serializable:
public class Club implements Serializable {
...
}
This tells the JVM that the class can be serialized to a stream. You don't have to implement any method, since this is a marker interface.
To write your list to a file do the following:
FileOutputStream fos = new FileOutputStream("t.tmp");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(clubs);
oos.close();
To read the list from a file, do the following:
FileInputStream fis = new FileInputStream("t.tmp");
ObjectInputStream ois = new ObjectInputStream(fis);
List<Club> clubs = (List<Club>) ois.readObject();
ois.close();
As an exercise, I would suggest doing the following:
public void save(String fileName) throws FileNotFoundException {
PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
for (Club club : clubs)
pw.println(club.getName());
pw.close();
}
This will write the name of each club on a new line in your file.
Soccer
Chess
Football
Volleyball
...
I'll leave the loading to you. Hint: You wrote one line at a time, you can then read one line at a time.
Every class in Java extends the Object class. As such you can override its methods. In this case, you should be interested by the toString() method. In your Club class, you can override it to print some message about the class in any format you'd like.
public String toString() {
return "Club:" + name;
}
You could then change the above code to:
public void save(String fileName) throws FileNotFoundException {
PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
for (Club club : clubs)
pw.println(club); // call toString() on club, like club.toString()
pw.close();
}
In Java 8 you can use Files.write() method with two arguments: Path and List<String>, something like this:
List<String> clubNames = clubs.stream()
.map(Club::getName)
.collect(Collectors.toList())
try {
Files.write(Paths.get(fileName), clubNames);
} catch (IOException e) {
log.error("Unable to write out names", e);
}
This might work for you
public void save(String fileName) throws FileNotFoundException {
FileOutputStream fout= new FileOutputStream (fileName);
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(clubs);
fout.close();
}
To read back you can have
public void read(String fileName) throws FileNotFoundException {
FileInputStream fin= new FileInputStream (fileName);
ObjectInputStream ois = new ObjectInputStream(fin);
clubs= (ArrayList<Clubs>)ois.readObject();
fin.close();
}
ObjectOutputStream.writeObject(clubs)
ObjectInputStream.readObject();
Also, you 'add' logic is logically equivalent to using a Set instead of a List. Lists can have duplicates and Sets cannot. You should consider using a set. After all, can you really have 2 chess clubs in the same school?
To save and load an arraylist of
public static ArrayList data = new ArrayList ();
I used (to write)...
static void saveDatabase() {
try {
FileOutputStream fos = new FileOutputStream("mydb.fil");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(data);
oos.close();
databaseIsSaved = true;
}
catch (IOException e) {
e.printStackTrace();
}
} // End of saveDatabase
And used (to read) ...
static void loadDatabase() {
try {
FileInputStream fis = new FileInputStream("mydb.fil");
ObjectInputStream ois = new ObjectInputStream(fis);
data = (ArrayList<User>)ois.readObject();
ois.close();
}
catch (IOException e) {
System.out.println("***catch ERROR***");
e.printStackTrace();
}
catch (ClassNotFoundException e) {
System.out.println("***catch ERROR***");
e.printStackTrace();
}
} // End of loadDatabase

Serialize - failing after package renaming

Appended are my little utility functions for serialising objects. I just encountered following problem:
I renamed a package and suddenly I get a java.lang.ClassCastException when opening my app and trying to read serialised data...
Can I somehow solve that? I would like my serialisations to be working after a renaming, can I do something to implement this? Via some versioning for example?
Here are my two simple functions I use currently:
public static String serialize(Object object)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try
{
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(object);
oos.flush();
oos.close();
} catch (IOException e)
{
L.e(StringUtil.class, e);
}
return Base64.encodeToString(baos.toByteArray(), 0);
}
public static <T> T deserialize(String serializedObject, Class<T> clazz)
{
if (serializedObject == null)
return (T)null;
byte [] data = Base64.decode(serializedObject, 0);
Object o = null;
try
{
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
o = ois.readObject();
ois.close();
}
catch (IOException e)
{
L.e(StringUtil.class, e);
}
catch (ClassNotFoundException e) {
L.e(StringUtil.class, e);
}
return (T)o;
}
I can suggest next options:
add support to your deserialize method to deal with old package names
convert byte [] data to String
replace old package name with new in deserialized data (with regexp)
continue to deserialize with ObjectInputStream

Serialize multiple entries to the same file

I'm basically making a journal app where each individual journal entry needs to persist, and I would like to keep all entries in a single file.
I've seen tons of tutorials on serializing a single object and so I came up with this solution, (which doesn't work) but even if I manage to fix it, it feels like a sloppy solution.
(Here I'm trying to serialize an arraylist, and each time I save an entry, i de-serialize the list and add the new entry to the list before serializing again)
To clarify, my question is: s this a good way to save objects to the same file, on multiple occasions?
Or does anyone have some tips about something else I should try, links to videos or documentation regarding this is also appreciated.
public class Serializer
{
//Calls readFile and adds the returned entries to an ArrayList
//Add the target object to the list and write to the file
public static void writeToFile(Object target)
{
ArrayList entries = new ArrayList();
entries = readFile();
entries.add(target);
String filename = "entries.bin";
FileOutputStream fileOut = null;
ObjectOutputStream objOut = null;
try
{
fileOut = new FileOutputStream(filename);
objOut = new ObjectOutputStream(fileOut);
objOut.writeObject(entries);
objOut.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
//Reads the file and returns all entries in a list
public static ArrayList readFile ()
{
ArrayList persistedEntries = new ArrayList<>();
String filename = "entries.bin";
FileInputStream fileIn = null;
ObjectInputStream objIn= null;
try
{
fileIn = new FileInputStream(filename);
objIn = new ObjectInputStream(fileIn);
persistedEntries = (ArrayList) objIn.readObject();
objIn.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
return persistedEntries;
}
}
Is this a good way to save objects to the same file, on multiple occasions?
I would argue no. This is because your method writeToFile or more accurately appendToFile can introduce strange behaviour in edge cases (such as entries.bin having an unexpected object). I would argue for this:
Use writeToFile(ArrayList<Object> target) to overwrite the file with the specified array. Then add a method appendToFile(Object target) that handles the process of reading from entries.bin from the disk, appending target then writing the array to the disk. This has the advantage of separating any logic related to 'merging' the new object target with the file on disk, and the actual logic of writing to the entries.bin file.
If just a learning exercise I would go with the above. Potential resource
Adding a reformatted version:
public class Serializer
{
private String filename;
// pass in "entries.bin"
public Serializer(String filename) {
this.filename = filename;
}
public void append(Object target) {
// readfile will return at least empty arraylist
ArrayList entries = readFile();
entries.add(target);
serialize(entries);
}
public void serialize(ArrayList entries)
{
FileOutputStream fileOut = null;
ObjectOutputStream objOut = null;
try
{
fileOut = new FileOutputStream(filename);
objOut = new ObjectOutputStream(fileOut);
objOut.writeObject(entries);
objOut.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
//Reads the file and returns all entries in a list
public ArrayList deserialize ()
{
ArrayList persistedEntries = new ArrayList<>();
FileInputStream fileIn = null;
ObjectInputStream objIn = null;
try
{
fileIn = new FileInputStream(filename);
objIn = new ObjectInputStream(fileIn);
Object result = objIn.readObject();
if (!(result instanceof ArrayList)) {
// read object is not an arraylist
}
persistedEntries = (ArrayList) objIn.readObject();
objIn.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
return persistedEntries;
}
}

Categories