I'm trying to write an ArrayList to a file in java. This is what I do in main class:
To write Strings into the ArrayList I do:
list.add(String);
Then, to write it to the file:
readWrite.writing(list);
list is: List<String> list = new ArrayList<String>();
readWrite references to this class where I have defined the methods to read/write to a file:
public void writing(ArrayList listToWrite) throws IOException {
fileOutPutStream = new FileOutputStream (file);
write = new ObjectOutputStream (fileOutPutStream);
for (int i=0; i<=listToWrite.size(); i++){
write.writeObject(listToWrite.get(i));
}
write.close();
}
When trying it on the console, I'm getting this exception:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at //*I GET REFERENCED TO THIS LINE IN THE CODE ABOVE:* **write.writeObject(listToWrite.get(i));**