Reputation: 3564
I'm starting with java, and now I'm doing some exercises on read/writing files.
I write strings with this format:
String wordList: word1 word2 word3; word4 word5 word6; code
Then I write this to the file using this code:
public void writeSelling(String wordList) throws IOException {
fileOutPutStream = new FileOutputStream (file);
write= new ObjectOutputStream (fileOutPutStream);
write.writeObject(wordList);
write.close();
contador++;
}
But where I'm not getting able to do it right is when reading it. For now, what I get is a null when reading the content of the file, so I think that I'm doing something wrong on the method.
This is the method I use to read the 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;
}
I call this method this way on the main file:
public static void listSelling(){
ArrayList objects;
try{
objects = sellingObject.readSelling();
for (Iterator it = sellingObject.iterator(); it.hasNext();) {
String s = (String)it.next();
System.out.println(s.toString());
}
}catch(FileNotFoundException fnfe){
System.out.println(fnfe.getMessage());
}catch(IOException ioe){
System.out.println(ioe.getMessage());
}catch(Exception e){
System.out.println(e.getMessage());
}
}
I don't have knowledge enough to work with the Iterator, so maybe I'm not using it right.
UPDATE -- Definition of "file.dat
This file is defined this way in other class:
private final String file;
public WriteReadObject(String file){
this.file= file;
}
Then in the main file is called this way:
static WriteReadObject selling= new WriteReadObject("file.dat");
UPDATE 2 --
I see that when I'm writing to the file, I'm writing a null value, and here is where it fails.
I have this:
String one = word1 word2 word3
String two = word4 word5 word6
Before call the write method to write on the file, I add these 2 strings in another string to get only one string. To do this I've created this method:
public String add(String c, String m){
sellinglist[contador] = c + m;
contador++;
String list= sellinglist[contador];
return list;
}
Where c is string one and m y string two
Upvotes: 0
Views: 2569
Reputation: 5220
The problem is you are writing single object and try to read an array of objects. Every time you are writing object you rewrite current file. Change openning of output stream to append data to file (but don't forget to clear it when writing first object):
fileOutPutStream = new FileOutputStream (file, contador != 0);
Upvotes: 1
Reputation: 12122
alexey28 said the right thing - you're rewriting the file and finally there's only the last insertion. Anyway it's not that simple to just change FileOutputStream
argument to make it work - you can't just append to an ObjectOuputStream
- if you would like to, see here. It will corrupt the stream what will result in StreamCorruptedException.
The best solution would be to open ObjectOutputStream
once at the begining, write all objects you want and then close stream.
It all depends on how you receive data which are to be written (If you are writing strings then it would be probably more comfortable to do it not in binary mode but text - here is tutorial which explains how to do that).
If you want code how to simply write list of Strings then you can try this:
/* writing */
public void writeSelling(List<String> wordLists) throws IOException {
fileOutPutStream = new FileOutputStream (file);
write= new ObjectOutputStream (fileOutPutStream);
for (String s : wordLists) {
write.writeObject(s);
}
write.close();
contador++;
}
Now you can change code in the place where you call writeSelling()
.
/* calling method */
List<String> wordLists = new ArrayList<String>();
{ // it's probably loop
String wordList = // somehow receive list like word1 word2 word3; word4 word5 word6; code
wordLists.add(wordList);
}
writeSelling(wordLists);
The rest remains the same. Don't call writeSelling()
method multiple times, just once.
Upvotes: 1