Read many ArrayList Objects out of File with ObjectInputStream? - java

i write ArrayLists into a file. I read it with FileInputStream. But always just the "first" ArrayList is appears by reading. I tried it with readInt() / wirteInt(), and loops, but there were always thrown exceptions, by calling readInt() --> EOF
I want to read all ArrayList from this File into an ArrayList. My application needs to be persistent, so i serialized the ArrayLists.
write into file:
try {
FileOutputStream fos = new FileOutputStream(_cache, true);
ObjectOutputStream os = new ObjectOutputStream(fos);
// os.writeInt(newValueList.size()); // Save size first
os.writeObject(newValueList);
os.flush();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
read from file:
List cachedValueList = new ArrayList<String>();
ObjectInputStream o = new ObjectInputStream(new FileInputStream("caching.io"));
// int count = o.readInt(); // Get the number of regions
try {
cachedValueList.add(o.readObject());
} catch (EOFException e) {
o.close();
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

If you are writing the same list many times, it will only write the list once. After that it will use the same reference but the contents will be the same (this is true of any object)
If you call reset() between writeObject() it will send a fresh copy of the list each time.

Related

Workaround java.io.EOFException cause by ObjectInputStream [duplicate]

This question already has answers here:
java.io.FileNotFoundException when creating FileInputStream
(2 answers)
Closed 6 years ago.
For my application I want to use a Map to act as a database. To save and load a map, I am writing/reading it to/from database.ser using this 2 methods:
private synchronized void saveDB() {
try {
fileOut = new FileOutputStream(db);
out = new ObjectOutputStream(fileOut);
out.writeObject(accounts);
fileOut.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
#SuppressWarnings("unchecked")
private void loadDB() {
try {
fileIn = new FileInputStream(db);
in = new ObjectInputStream(fileIn); // that is where error is produced if fileIn is empty
accounts = (Map<String, Client>) in.readObject();
in.close();
fileIn.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I want to load into Map when application starts, so I invoke method in constructor like this:
protected DriveatorImpl() {
accounts = new ConcurrentHashMap<String, Client>();
db = new File("C:/Users/eduar/git/Multy-Threaded-Bank-System/Bank-Services/database.ser");
// also, any suggestions how can I make path to a file more flexible in case I want to run Server side of an app on different machine?
if (!db.exists()) {
try {
db.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
loadDB(); // loads database when server start
}
I am aware of what causing an error, but I don't know what should I change in my design to avoid ObjectInputStream constructor receiving empty stream!
Any suggestions on what I can do differently?
Edit: I want to note that in fresh application run database.ser is empty since there was no entries made into Map yet.
Thank You!
First why the EOFExcpetion occur?
There are no contents in file or file is empty and you tried to read file.
You can avoid the EOFException for an empty file by checking file content length if it is less than or equal to zero means file is empty. another way to check if file is empty
Some code change and it worked for me.
#SuppressWarnings("unchecked")
private void loadDB() {
try {
if (db.length() <= 0) {
// if statement evaluates to true even if file doesn't exists
saveDB(); // save to a file an empty map
// if file doesn't exist, it creates a new one
// call loadDB inside constructor
}
FileInputStream fileIn = new FileInputStream(db);
ObjectInputStream in = new ObjectInputStream(fileIn); // that is where error is produced if fileIn is empty
in.readObject();
in.close();
fileIn.close();
System.out.println(accounts);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Get rid of the file.exists()/file.createNewFile() crap. All it is doing for you is masking the original FileNotFoundException problem, and turning into a thoroughly predictable EOFException because of trying to construct an ObjectInputStream around an empty stream. Handle the original problem. Don't just move it, or turn it into something else.

How the FileOutputStream write properties file don't lost information

I found something wrong when I write the Properties file by using FileOutputStream.
public synchronized static void setProperties(String file,String Properties,String value)
{
try {
FileInputStream is = new FileInputStream(file);
Properties proper = new Properties();
proper.load(is);
proper.setProperty(Properties.toUpperCase(), value);
is.close();
FileOutputStream os = new FileOutputStream(file);
proper.store(os,"Update the file:"+Properties);
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Look at the two lines below:
FileOutputStream os = new FileOutputStream(file);
proper.store(os,"Update the file:"+Properties);
When the first line executed, the file will be empty, until the second line execute finished. Now, I assume the second line will execute within 3 seconds. During this period, the program crashed or another reason lead to the file to be unsuccessfully written. I will get an empty Properties file when I'm running my program next time. Anyone can tell me how to prevent this kind of situation to occur?
I changed my program like below, Seems it more better than before. At least I won't get a empty properties file, Thanks all guys.
public synchronized static void setProperties(String file,String Properties,String value)
{
try {
FileInputStream is = new FileInputStream(file);
Properties proper = new Properties();
proper.load(is);
proper.setProperty(Properties.toUpperCase(), value);
is.close();
proper.store(new FileOutputStream(file+".tmp"),"Update the file:"+Properties); //Prevent empty file
File old = new File(file);
File tmp = new File(file+".tmp");
if(tmp.exists() && tmp.length()>0)
{
old.renameTo(new File(file+".old"));
tmp.renameTo(new File(file));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Java file writing with append rather than overwriting?

I have this code that creates a file and saves the users input, but it keeps overwriting and I want it to save each entry a user gives. How can I do this?
File file = new File("info.txt");
BufferedWriter output = null;
try {
output = new BufferedWriter(new FileWriter(file));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
output.write("Users pick: " + myint+ "\t");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//file writer
Change
output = new BufferedWriter(new FileWriter(file));
to
output = new BufferedWriter(new FileWriter(file,true));
for opening the file in append mode.

Java readObject previews data, but will not read or transfer it [duplicate]

This question already has answers here:
How to prevent InputStream.readObject() from throwing EOFException?
(3 answers)
Closed 9 years ago.
I can't seem to find a way to make readObject() transfer it's contents to an object variable. When I step through the Load function I get to "temp = (HashMap) ois.readObject();" Before this line is executed I am able to see the HashMap's data that I've written with oos in the expressions window of Eclipse so I know the data is there, however when this line executes I'm jumped to the IOException catch with an EOF. From what I've read this is expected, but I have not found a way to catch the EOF (loops with available() and readObjectInt() did not work). I'm running this on an Android emulator. Any suggestions would be appreciated.
public void Save(Pottylog data)
{
try
{
FileOutputStream fos = openFileOutput("Plog", Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(data.get());
oos.close();
}
catch (FileNotFoundException ex)
{
ex.printStackTrace();
}
catch (java.io.IOException e)
{
e.printStackTrace();
}
}
public HashMap<String, Integer> Load()
{
HashMap<String, Integer> temp = null;
try
{
FileInputStream fis = openFileInput("Plog");
ObjectInputStream ois = new ObjectInputStream(fis);
temp = (HashMap<String, Integer>) ois.readObject();
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return temp;
}
EOFException means you have reached the end of the stream. I don't know what you think you're seeing in the debugger, but there is no object there in the stream to be read. catch(EOFException exc) does work; at this point you should close the stream and exit the reading loop. Don't misuse available() as an end of stream test: that's not what it's for.

How save a string to a file in Android to be able to read it later?

Im trying to implement the section "Using the Internal Storage" in http://developer.android.com/guide/topics/data/data-storage.html
I think I did the first part correctly, saving string to a file, but how do I read the string later?
Her is what my code looks like:
String FILEPROFILE = "profileinfo";
FileOutputStream fos = null;
BufferedInputStream fis = null;
OutputStream out = null;
try {
fos = openFileOutput(FILEPROFILE, Context.MODE_PRIVATE);
fos.write(profile.toString().getBytes());
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fis = new BufferedInputStream(new FileInputStream(FILEPROFILE));
Log.d("UsersThoughts", "BufferedInputStream is " + fis.read());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
EDIT:
I change it to this:
try {
fis = openFileInput(FILEPROFILE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Log.d("UsersThoughts", "This blah object read " + fis.read());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("UsersThoughts", "This blah object trying to read: " + e.toString());
}
try{
Log.d("UsersThoughts", "This blah object toString " + fis.toString());
} catch (Exception e)
{
e.printStackTrace();
Log.e("UsersThoughts", "This blah object trying to make string: " + e.toString());
}
The output in the logcat looks like this:
11-15 18:41:34.862: D/UsersThoughts(7777): This blah object read123
11-15 18:41:34.862: D/UsersThoughts(7777): This blah object toString java.io.FileInputStream#46356128
Looks like it's reading the files as bites....how do I get the text back that I copied?
Follow the answer i gave here to store a String to a file in Android: Should I store data in sqlite in case user is offline?
I'm quoting this from the same source you've cited:
To read a file from internal storage:
Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream.
Read bytes from the file with read().
Then close the stream with close().
EDIT: For example,
FileInputStream fis = openFileInput("FILENAME");
BufferedInputStream blah = new BufferedInputStream(fis);
Then do whatever you were doing earlier with this blah object.

Categories