I need to write strings on an file on eclipse without overwriting old strings. The function would be something like: create a string, save it on the file, create another string, save it on the file, this with several strings.
The strings have the next format:
String one = name surname surname; value1 value2 value3; code
So the method will be: create string, save it on the file. Create another string, save it on the file, etc.. Then after saving the wanted amount of strings on the file, I would need to read the file listing all the strings on the console.
But for now, I only get to save one string on the file and then list it. If I save two strings, the second overwrites the first, and anyway, it doesn't it do right because returns me null value when I want to list them.
This is the method which writes string on file:
public void writeSelling(List<String> wordList) throws IOException {
fileOutPutStream = new FileOutputStream (file);
write= new ObjectOutputStream (fileOutPutStream);
for (String s : wordList){
write.writeObject(s);
}
write.close();
}
This is how I call write method on the main class:
List<String> objectlist= new ArrayList<String>();
objectlist.add(product); //Product is the string I save each time
//which has the format I commented above
writeSelling(objectlist);
This is the method which reads strings from file:
public ArrayList<Object> readSelling() throws Exception, FileNotFoundException, IOException {
ArrayList<Object> objectlist= new ArrayList<Object>();
fileInPutStream = new FileInputStream (file);
read= new ObjectInputStream (fileInPutStream);
for (int i=0; i<contador; i++){
objectlist.add(read.readObject());
}
read.close();
return objectlist;
}
And this is how I call read on the main class:
ArrayList sellingobjects;
sellingobjects= readSelling();
for (Iterator it = sellingobjects.iterator(); it.hasNext();) {
String s = (String)it.next();
}
System.out.println(s.toString());