EDIT: The error was caused by Netbeans not the code. Post has been edited to show all the code since the Git files are being removed.
I have a group project at school to design a tower defense game and I have been attempting to add Serialization.
Here is the code:
public class Serialization implements Serializable {
private static FileOutputStream file;
private static ObjectOutputStream write;
public static boolean checkFile(String name){
boolean check = false;
check = new File("log",name+".ser").isFile();
return check;
}
public static void createFile(String name) {
try {
file = new FileOutputStream("log/"+name+".ser");
} catch (FileNotFoundException ex) {
Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static ObjectOutputStream openFile(String name) {
try {
file = new FileOutputStream("log/"+name+".ser");
write = new ObjectOutputStream(file);
} catch (FileNotFoundException ex) {
Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
}
return write;
}
public static void addLine(ObjectOutputStream con,Object data) {
try {
con.writeObject(data);
} catch (IOException ex) {
Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void readLine(String name){
FileInputStream fileIn = null;
try {
fileIn = new FileInputStream("log/"+name+".ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
System.out.println(in.readObject());
in.close();
fileIn.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fileIn.close();
} catch (IOException ex) {
Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void closeFile(ObjectOutputStream con){
try {
con.close();
} catch (IOException ex) {
Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
I eventually abandoned it for two reasons. One it was overkill (trying to have a central class that handles multiple serialization files) and two I could not get it to work with what I am about to talk about.
What I really wanted was to serialize the enemyArray. This holds every living enemy in the game and all their information (stats). If you scroll down to line 192 that is what is left of my serialization attempts:
public void serialize(){
if (Serialization.checkFile("test")==false){
Serialization.createFile("test");
}
ObjectOutputStream connection = Serialization.openFile("test");
for (int x=0;x<enemyArray.length;x++){
if (enemyArray[x]!=null){
Serialization.addLine(connection,enemyArray[x]);
}
}
//Serialization.addLine(connection,enemyArray);
Serialization.closeFile(connection);
enemyArray = null;
Serialization.readLine("test");
//System.out.println(enemyArray[0].id);
// try {
// FileOutputStream fileOut = new FileOutputStream("log/test.ser");
// ObjectOutputStream out = new ObjectOutputStream(fileOut);
// out.writeObject(enemyArray);
// out.writeUTF(t);
// // Do work here
// for (int x=0;x<enemyArray.length;x++){
// if (enemyArray[x]!=null){
// Enemy tmp = enemyArray[x];
// System.out.println(tmp+" >>> "+enemyArray[x]);
// out.writeObject(tmp);
// }
// }
// out.close();
// fileOut.close();
// } catch (FileNotFoundException ex) {
// Logger.getLogger(EnemyController.class.getName()).log(Level.SEVERE, null, ex);
// } catch (IOException ex) {
// Logger.getLogger(EnemyController.class.getName()).log(Level.SEVERE, null, ex);
// }
}
}
It will create the file but will not save anything to it. What is wrong with the code? from what I studied enemyArray shouldn't be static so I removed that and still no change.
Try doing a out.flush(); brefore you close thre output stream.
I found the error after some troubleshooting in the computer lab with my group. Hopefully this can help anyone who finds themselves in my situation or in a similar Java file read/ write issue.
My IDE, Netbeans, shows the file in the files and project list once it is dynamically created, but because it was filled with serialized data and was created after the project was opened and run it would only display an empty file when in reality there is actually data inside the file. String data is fine so it may only be certain data types (like serialized objects) that trigger this error.
Several different versions of my code were correct but they all seemed broken because an empty file was returned to the IDE. The solution is two fold:
Manually check it through your file explorer. It will show a size and can be opened in another editor but may still be buggy in the IDE you ran the program in.
Close down your IDE and on reopening, at least in Netbeans case, the file should be updated correctly.
On a similar note Netbeans may just all together keep showing the empty file instead of offering you the option to select the files encoding. Just view the file in your explorer in this case and if the size is greater than 0 you know your code is ok. This is purely an IDE error.
Related
something is really messed up. I've got a ".ser" document in the assets folder, which stores an ArrayList of Objetcs. In an android application, I want to read this objects. There are a lot of posts related to this issue, however none of them could solve my problem. The strange part is, when I am using similar code in non - android context / "normal" java, it works properly. Here, the last line throws a NullPointerException - What is going wrong?
public void getData() {
ArrayList<MyClass> output= null;
InputStream is = null;
ObjectInputStream ois = null;
try{
is = this.getAssets().open("data.ser");
ois = new ObjectInputStream(is);
output = (ArrayList<MyClass>)ois.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Log.d("TAG", output.get(0).getId());
}
I would create a class and place the array within a single object:
public class ListObjects implements Serializable {
List<MyClass> listMyClass = new ArrayList<>();
public ListObjects(){
}
public List<MyClass> getListMyClass() {
return listMyClass;
}
public void setListMyClass(List<MyClass> listMyClass) {
this.listMyClass = listMyClass;
}
}
I had a similar problem. And it was because the name of the package in the java app was not called the same as the package name in android. And therefore I did not recognize them as equal objects. This is how I do it:
public static Object fromData(byte[] data) {
ObjectInputStream ois = null;
Object object = null;
try {
ois = new ObjectInputStream(
new ByteArrayInputStream(data));
object = ois.readObject();
} catch (Exception ex) {
Logger.getLogger(ModeloApp.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
ois.close();
} catch (Exception ex) {
Logger.getLogger(ModeloApp.class.getName()).log(Level.SEVERE, null, ex);
}
}
return object;
}
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.
-The array of appliances(clock,lamp or television) is displayed on an array of labels (pictures[]) in a 3x3 grid
-An example of outputting an appliance icon (which is when a device is added to the grid though a button labelled 'add device')
Television myTelevision = new Television();
appliance[count-1] = myTelevision;
pictures[count-1].setIcon(appliance[count-1].getPicture());
This is my code for saving the array of appliances (objects) to a file and reading them back in ( re-populating the appliance array):
if(e.getSource()==but3)
{
ObjectInputStream input
= null;
try {
input = new ObjectInputStream(
new FileInputStream("livingroom.bat"));
Appliance[] appliance = (Appliance[]) (input.readObject());
} catch (IOException ex) {
Logger.getLogger(HomeController.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex)
{
Logger.getLogger(HomeController.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
input.close();
} catch (IOException ex) {
Logger.getLogger(HomeController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
if(e.getSource()==but4)
{
ObjectOutputStream output
= null;
try {
output = new ObjectOutputStream(
new FileOutputStream("livingroom.bat", true));
output.writeObject(appliance);
} catch (IOException ex) {
Logger.getLogger(HomeController.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
output.close();
} catch (IOException ex) {
Logger.getLogger(HomeController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
After deserialization I am trying to display the imageicon for the appliances in the array (which has just been re-populated). However no matter how I try nothing seems to happen (the grid of pictures shows no change).
What I am asking for: Can anyone tell me a method of applying the image icons for the appliances to the grid of pictures once deserialization has taken place?
You are deserialising into a local variable whose scope ends with the try block it is declared in, and you don't do anything with the local variable, so nothing happens.
Presumably you should be deserialing into a member variable.
I'm a moderately-experienced C++ guy slowly learning Java. I'm writing a program which needs to do the following:
Create a simple text file, default directory is fine
As the program runs, periodically write one line of data to the file. Depending on a number of factors, the program may write to the file once or a million times. There is no way of knowing which write will be the last.
I've been researching different ways to do this, and this is the working code I've come up with. There are two files, "PeteProgram.java" and "PeteFileMgr.java" :
/*
"PeteProgram.java"
*/
import java.io.*;
import java.lang.String;
public class PeteProgram {
public static void main(String[] args) throws IOException {
String PeteFilename="MyRecordsFile.txt";
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(PeteFilename), "utf-8"));
PeteFileMgr MyPeteFileMgr = new PeteFileMgr(writer);
MyPeteFileMgr.AddThisString(writer, "Add this line #1\n");
MyPeteFileMgr.AddThisString(writer, "Add this line #2\n");
MyPeteFileMgr.AddThisString(writer, "Add this line #3\n");
}
}
//=====================================================================================================
//=====================================================================================================
/*
"PeteFileMgr.java"
*/
import java.io.*;
public class PeteFileMgr {
public PeteFileMgr(Writer writer) {
try {
writer.write("File created!");
} catch (IOException ex) {
// report
} finally {
try {writer.close();} catch (Exception ex) {}
}
}
void AddThisString(Writer writer, String AddThis) {
try {
writer.append(AddThis);
} catch (IOException ex) {
// report
} finally {
try {writer.close();} catch (Exception ex) {}
}
}
}
The initial creation of the file works just fine. However, the to-be-added lines are not written into the file. Because the program compiles and runs with no errors, I assume the program tries to write the added lines, fails, and throws an exception. (Unfortunately, I am working with a primitive compiler/debugger and can't see if this is the case.)
Does anyone spot my mistake?
Many thanks!
-P
That's because you're not flushing the Writer. You should call flush from time to time. Also, you should close your Writer at the end of your app, not after writing content into it. close method automatically flushes the contents of the writer.
So, this is how your code should look like:
public class PeteProgram {
public static void main(String[] args) {
String peteFilename = "MyRecordsFile.txt";
//here's when the physical file is created
Writer writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(peteFilename), "utf-8"));
PeteFileMgr peteFileMgr = new PeteFileMgr(writer);
peteFileMgr.addThisString(writer, "Add this line #1\n");
peteFileMgr.addThisString(writer, "Add this line #2\n");
peteFileMgr.addThisString(writer, "Add this line #3\n");
} catch (IOException e) {
//handle the exception
//basic handling
e.printStacktrace();
} finally {
//this is a must!
try { writer.close(); } catch(IOException silent) { }
}
}
}
public class PeteFileMgr {
public PeteFileMgr(Writer writer) {
try {
//this method is not creating the physical file
writer.write("File created!");
} catch (IOException ex) {
// report
} finally {
//remove this call to close
//try {writer.close();} catch (Exception ex) {}
}
}
public void addThisString(Writer writer, String addThis) {
try {
writer.append(addThis);
} catch (IOException ex) {
// report
} finally {
//remove this call to close
//try {writer.close();} catch (Exception ex) {}
}
}
}
Or if using Java 7 or superior using the try-with-resources:
public class PeteProgram {
public static void main(String[] args) {
String peteFilename = "MyRecordsFile.txt";
//here's when the physical file is created
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(peteFilename), "utf-8"))) {
PeteFileMgr peteFileMgr = new PeteFileMgr(writer);
peteFileMgr.addThisString(writer, "Add this line #1\n");
peteFileMgr.addThisString(writer, "Add this line #2\n");
peteFileMgr.addThisString(writer, "Add this line #3\n");
} catch (IOException e) {
//handle the exception
//basic handling
e.printStacktrace();
}
}
}
I have a program that needs to load data at launch. The data comes from a serialized object. I have a method loadData(), which is called upon construction of the Data class. Sometimes, (I.e. after a loss of saveData, or on first program launch on a new system), the file can be empty. (The file will exist though, the method ensures that).
When I try to run the program, I recieve an EOFException. So, in the method, I try to catch it, and just print a line to the console explaining what happened and return to the caller of the method. (so, upon return, the program will think loadData() is complete and has returned. However, it still crashes throwing the exception without printing a line to the console or anything. It is like it is totally ignoring the catch I have in place.
CODE:
protected void loadData()
{
// Gets/creates file object.
saveFileObject = new File("savedata.ser");
if(!saveFileObject.exists())
{
try
{
saveFileObject.createNewFile();
}
catch(IOException e)
{
System.out.println("Uh oh...");
e.printStackTrace();
}
}
// Create file input stream
try
{
fileIn = new FileInputStream(saveFileObject);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
// Create object input stream
try
{
inputStream = new ObjectInputStream(fileIn);
}
catch(IOException e)
{
e.printStackTrace();
}
// Try to deserialize
try
{
parts = (ArrayList<Part>)inputStream.readObject();
}
catch(EOFException e)
{
System.out.println("EOFException thrown! Attempting to recover!");
return;
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
// close input stream
try
{
inputStream.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
Any help please?
Try writing your code like :
protected void loadData() {
// Gets/creates file object.
saveFileObject = new File("savedata.ser");
try {
if (!saveFileObject.exists()) {
saveFileObject.createNewFile();
}
// Create file input stream
fileIn = new FileInputStream(saveFileObject);
// Create object input stream
inputStream = new ObjectInputStream(fileIn);
// Try to deserialize
parts = (ArrayList<Part>) inputStream.readObject();
// close input stream
inputStream.close();
} catch (EOFException e) {
System.out.println("EOFException thrown! Attempting to recover!");
} catch (IOException e) {
System.out.println("Uh oh...");
e.printStackTrace();
}
}
Also note that EOFException is a sub-class of IOException
How about making one try and then making catches respectively like here?