I'm completely new to programming, so I'm having a difficult time resolving my own errors. Someone advised me to try it on this website, so I thought why not give it a shot.
The other posts that I found regarding this error didn't seem very relevant: most were people advising to close the input stream, but my code already does that.
What I want it to do: Write a Photo object called "photo" to a file called "test.ser". Then read the file "test.ser" AND return the path of the object ("photo") in "test.ser" back to me.
What it actually does: Writes a Photo object called "photo" to "test.ser". Reads "test.ser", returns an EOFException and no path.
Returning the path isn't actually very important, as long as it returns something of value to me. But I am getting the same error when I use "System.out.println(photo)" or "photo.getId()".
I'm not very sure what I need to paste here, so I will post the two try/catch-es that I use for serializing and deserializing the object:
Serializing object:
File test = new File("path.../something.ser");
Photo photo = new Photo(2, "..\\images\\2.jpg", getImage("..\\images\\2.jpg"));
try {
FileOutputStream fos = new FileOutputStream(test);
ObjectOutputStream out = new ObjectOutputStream(fos);
if (!test.exists()) {
test.createNewFile();
}
out.writeObject(photo);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
Deserializing object:
try {
FileInputStream fis = new FileInputStream(test);
ObjectInputStream in = new ObjectInputStream(fis);
in.readObject();
photo = (Photo)in.readObject();
photo.getPath();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
And the error:
run:
null
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2571)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1315)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at view.Main.<init>(Main.java:103)
//103 is the line that casts the input object to a Photo object.
BUILD SUCCESSFUL (total time: 1 second)
From what I unstander the error occurs when I am trying to type cast the object - that I receive through the method readObject - to a "photo" class object. At least, that's what the error at line 103 is refering to.
I read elsewhere that the error means that I "tried to read more objects than there actually are there". Not sure what that means though, because I just want it to read 1 image - which should be within the object - and return its location.
Also I read that ObjectInputStream never returns null, unless I gave that value somewhere. But it actually is returning(?) "null", even though my code doesn't contain a null value...
I've been at it for days now (yes I am just that bad) and still no luck.
You read it twice:
in.readObject();
photo = (Photo)in.readObject();
Remove the first line.
Also you don't have to create the file. The output stream will do that for you.
Related
I am creating a program for a small business. This program is meant to have smaller modules that, when developed, will be attatched to the rest of the program. It contains an "Article" and a "Category" class, which is contained in lists in a "ArticleDatabase" class.
This class is serialized and saved to a file to the harddrive.
The Register module is complete, and the "Receipt" class, is likewise contained within lists in a "RegisterDatabase" class, which is serialized and saved to a separate file.
System settings, are saved in the same manner.
However, now i am designing a Invoice module, and found out that i need to add a field to the "Article" class, and to the System data.
The register is now being used, and contains actual data that needs to be saved, and therefore i can't just change the class, since this gives an InvalidClassException when i load.
Since i know that this will be a common problem in the future too, i need some advice on how to tackle this problem.
How can i setup a system i which i can save a file from a class, and load the data into an updated or new version of this class, or should i approach this in an entirely new way?
I have tried loading the data form the old file in to a duplicate class with the needed fields addded, but reconfiguring the program to use the new files instead is a very cumbersome task, and if i have to do this every now and again, a lot of time will be wasted doing this.
The methods used for saving loading are as follows:
public void saveArticleDB() throws IOException {
// Write to disk with FileOutputStream
FileOutputStream f_out = new FileOutputStream("articles.data");
// Write object with ObjectOutputStream
ObjectOutputStream obj_out = new ObjectOutputStream(f_out);
obj_out.writeObject(MyMain.articleDB);
}
public ArticleDB loadArticleDB() throws IOException {
try {
FileInputStream f_in = new FileInputStream("articles.data");
ObjectInputStream obj_in = new ObjectInputStream(f_in);
Object obj = obj_in.readObject();
if (obj instanceof ArticleDB) {
return (ArticleDB) obj;
} else return null;
} catch (FileNotFoundException e) {
new MessageDialog("Article DB - File not found");
return null;
} catch (InvalidClassException e) {
new MessageDialog("Article DB - Class didnt match");
return null;
} catch (ClassNotFoundException e) {
new MessageDialog("Article DB - Class not found");
return null;
}
}
The classes that delivers data to the save file, implements Serializable, and thats the only code used regarding the saving and loading of the class.
This is my first attempt with serializing, saving and loading, which means i am quite new to this, and therefore know/understand very few of the concepts regarding these subjects.
Advice is much appreciated :-)
I'm trying to print objects into file.
Then I want to import them back to my program.
ObjectOutputStream not working, What am I missing? (try, catch not visible here but they're doing their job)
Map< Account, Customer> customerInfo = new HashMap< Account, Customer>();
File bankFile = new File("Bank.txt");
FileOutputStream fOut = new FileOutputStream( bankFile);
ObjectOutputStream objOut = new ObjectOutputStream(fOut);
for(Map.Entry<Account, Customer> e : bank.customerInfo.entrySet())
{
objOut.writeObject(e.getValue());
objOut.writeObject(e.getKey());
}
objOut.flush();
objOut.close();
fOut.close();
My problem here is that ObjectOutputStream is not working properly, it prints some weird code. I've used other methods to print out to file and they work just fine.
I've tried printing to different file extensions,
I tried changing the encoding for both the file and eclipse.
I tried different methods for getting the info from the Map using ObjectOutputStream. Is there a reason why ObjectOutputStream prints weird characters that I haven't think of? The entire file is almost impossible to read. Thanks!
Ps. some of the weird print, don't know if it helps.
¬ísrCustomerDìUðkJ
personalIdNumLnametLjava/lang/String;xpthellosr
SavingAccountUÞÀÀ;>ZfreeWithdrawDwithdrawalInterestRateLaccountTypeq~xrAccount é=UáÐI
accountNumberDbalanceDinterestRateLaccountTypeq~L transListtLjava/util/List;xpé?záG®{tsrjava.util.ArrayListxÒÇaIsizexpw
x?záG®{tSaving Accountq~sr
CreditAccountÝ
*5&VcLaccountTypeq~xq~ê?záG®{q~sq~ w
xtCredit Account
It's really quite simple. First things first, create a class that implements Serializable. Serializable is a marker interface, so you don't need to implement any methods for it:
public class Shoe implements Serializable { ... }
NOTE: If Shoe has other classes in it, for example Heel, or Buckle, those classes also need to implement the Serializable interface.
Next step is to write that to a file, using an ObjectOutputStream.
FileOutputStream out = new FileOutputStream("myfile.txt");
// Create the stream to the file you want to write too.
ObjectOutputStream objOut = new ObjectOutputStream(out);
// Use the FileOutputStream as the constructor argument for your object.
objOut.writeObject(new Shoe("Prada"));
// Write your object to the output stream.
objOut.close();
// MAKE SURE YOU CLOSE to avoid memory leaks, and make sure it actually writes.
There you have it. The serialized object is written to the txt file. Now to read it, it's just a case of using the ObjectInputStream.
ObjectInputStream objIn = new ObjectInputStream(new FileInputStream("myfile.txt");
Object obj = objIn.readObject();
if(obj instanceof Shoe)
{
Shoe shoe = (Shoe)obj;
}
And you've got an object you can use.
I get a file personHashMap.ser with a HashMap in it. Here's the code how i create it:
String file_path = ("//releasearea/ToolReleaseArea/user/personHashMap.ser");
public void createFile(Map<String, String> newContent) {
try{
File file = new File(file_path);
FileOutputStream fos=new FileOutputStream(file);
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(newContent);
oos.flush();
oos.close();
fos.close();
}catch (Exception e){
System.err.println("Error in FileWrite: " + e.getMessage());
}
}
Now i want, when the program is running, that all five minutes update the file personHashMap.ser only with the content which changed. So the method i called:
public void updateFile(Map<String, String> newContent) {
Map<String, String> oldLdapContent = readFile();
if(!oldLdapContent.equals(ldapContent)){ // they arent the same,
// so i must update the file
}
}
But now i haven't any ideas how i can realise that.
And is it better for the performance to update only the new content or should i clean the full file and insert the new list again?
Hope you can Help me..
EDIT:
The HashMap includes i.e street=Example Street.
But now, the new street called New Example Street. Now i must update the HashMap in the File. So i can't just append the new content...
Firstly HashMap isn't really an appropriate choice. It's designed for in-memory usage, not serialization (though of course it can be serialized in the standard way). But if it's just 2kb, then go ahead and write the whole thing rather than the updated data.
Second, you seem to be overly worried about performance of this rather trivial method (for 2kb the write will take mere milliseconds). I would be worried more about consistency and concurrency issues. I suggest you look into using a lightweight database such as JavaDB or h2.
Use the constructor FileOutputStream(File file, boolean append), set the boolean append to true. It will append the text in the existing file.
You can call the updateFile method in a loop and then call sleep for 5 minutes (5*60*1000 ms).
Thread.Sleep(300000); // sleep for 5 minutes
To append to your already existing file you can use :
FileOutputStream fooStream = new FileOutputStream(file, true);
I have a problem not to solve by myself.
I'm a biginner in JAVA.
I don't know solution about this problem. But I think that I know when this problem occurs.
So, I aleady have solution about this problem. But I want another solution because my solution has another problem too. I have to reduce the process time.
String intDir="C:\\RNE_IN";
while(true) {
File interfaceDirectory = new File(intDir);
String[] arrayfiles = interfaceDirectory.list(new FBMFileFilter());
for(String f : arrayfiles){
String filename = String.format("%1$s%2$s%3$s", intDir,File.separator,f);
File file = new File(filename);
FileInputStream stream = null;
System.out.println(file.canExecute()); // true
System.out.println(file.canRead()); // true
System.out.println(file.exists()); // true
System.out.println(file.isFile()); // true
System.out.println(file.length()); // call full bytes of file
// I can control NPE with this Thread sleep Time.
Thread.sleep(1);
// It occurs when Stream is constructed in the below.
stream = new FileInputStream(file);
FBMDeviceOnlyParser onlyparser = new FBMDeviceOnlyParser();
onlyparser.ParseDeviceNameOnly(stream);
String onlydevice = onlyparser.getDeviceName();
String onlystepseq = onlyparser.getStepSeq();
}
}
In above snippet, I think file has no problem.
file state is always true and file.length is full byte regardless Exception.
But, while infinite Loop, If I copy & paste from another Directory to the intDir , "NullPointerException" occurs.
When Thread.sleep(time) is over 1000ms, NPE doesn't occur.
I want to delete "Thread.sleep()" code because of process time.
If there are files in the intDir aleady before program start, Program has No problem (it doesn't occur NPE)
I want to check file or FileInputStream state not to occur NPE.
Thank you for your concern.
Your question is hard to understand, but I can tell you for a fact that it is impossible to get:
java.lang.NullPointerException at Apeiron.MainEntry.main(MainEntry.java:179) Exception in thread "main"
java.lang.NullPointerException at Apeiron.MainEntry.main(MainEntry.java:260)
if line 179 is this line:
stream = new FileInputStream(file);
One of the following must be:
you have given us an incomplete stack trace, or
you've told us the incorrect location of the exception, or
you are not actually executing that code at all; e.g. you've not rebuilt the code properly after changing it.
You probably run out of file handles sooner or later. Close the FileInputstream when you are finished with it.
Besides that, explain what you really want to do with your code (instead of driving CPU usage to the top).
How about this:
String intDir="C:\\RNE_IN";
File interfaceDirectory = new File(intDir);
while(true) {
for(File file : interfaceDirectory.listFiles(new FBMFileFilter())) {
System.out.println(file.canExecute()); // true
System.out.println(file.canRead()); // true
System.out.println(file.exists()); // true
System.out.println(file.isFile()); // true
System.out.println(file.length()); // call full bytes of file
final FileInputStream stream = new FileInputStream(file);
try {
FBMDeviceOnlyParser onlyparser = new FBMDeviceOnlyParser();
onlyparser.ParseDeviceNameOnly(stream);
String onlydevice = onlyparser.getDeviceName();
String onlystepseq = onlyparser.getStepSeq();
} finally {
stream.close();
}
}
}
I did a couple of things --
1. got rid of unnecessary file name generation
2. put a try finally to release file handle resources
BTW, my guess is that your sleep allowed finalizers to run.
I am a .NET Developer, but the question I am having is not related to .NET
Please keep this in mind even if my question sounds very trivial.
This is my question:
We have an swf in the browser, which communicates with a java extension
Its done using Smartfox Server(Used for MMO apllications)
From the swf we are grabbing a portion of the screen as "Byte Array" in action script(3).
And in Java, we are calling a function that converts the ByteArray to Image and then saves it.
Our Java developer is encountering the error
java.lang.illegalArgumentException
when the java function executes.
So basically, what I would like to know is this:
How to accept the object type Byte Array from ActionScript in Java?
Whats Java object type that is mapped to Byte Array in ActionScript?
The conversion part is easy, I dare say.
Update:
The code in the ActionScript Section
public function savePhoto(uName:String, ba:ByteArray, descr:String):void{
var obj:Object = {};
obj.arr = ba;
obj.desc = descr;
sfsConnectobj.photoSectionSave(obj,"save");
}
public function photoSectionSave(targetObject:Object,type:String) {
sfs.sendXtMessage("trialjava", "save", targetObject);
}
The first function calls the SmartFox Extension in Java.
The extension name is "trialjava.js"
The Java Code that accepts the function is
public void handleRequest(String cmd, ActionscriptObject ao, User u, int fromRoom)
{
try {
ActionscriptObject arr = ao.getObj("arr");
String dirName="C:\\";
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos;
oos = new ObjectOutputStream(bos);
oos.writeObject(ao.getObj("arr"));
oos.flush();
oos.close();
bos.close();
byte [] data = bos.toByteArray();
BufferedImage imag=ImageIO.read(new ByteArrayInputStream(data));
ImageIO.write(imag, "jpg", new File(dirName,"snap.jpg"));
}
catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Array reading not succesful. Error is: "+e);
}
}
Seems like there was a small mismatch in retrieving the objects by java.
Now the error is different.
Array reading not succesful. Error is:
java.io.NotSerializableException:
it.goto
andplay.smartfoxserver.lib.ActionscriptObject
Regards,
naveenj
flash.utils.ByteArray is mapped to Java's byte[] type.
I am not sure if this is an issue, but according to Flash security model, if SWF is loading media from any host/domain other that the one it was loaded, screen capture would result in error.
Can you check the byte array you received? What is its size? And try to print its starting few values.
Byte array is not received directly. It comes inside an AS object. The real question here is how to get this byte array inside the ActionScript object to a Java byte array object.
I am the aforesaid Java developer and I am doing this.