This program is run when there is no extant file named "Test Object.to". The output of this program is "IO caught". Why?
import java.io.*;
public class Test
{
public static void main (String [] args)
{
TestObject testObject = new TestObject("Test Object");
try
{
saveTestObject(testObject);
}
catch (FileNotFoundException fnf)
{
System.out.println("FNF caught");
}
catch (IOException io)
{
System.out.println("IO caught");
}
}
static void saveTestObject(TestObject to) throws FileNotFoundException, IOException
{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(to.name + ".to"));
oos.writeObject(to);
oos.close();
}
}
class TestObject
{
String name;
TestObject(String s)
{
name = s;
}
}
First of all, this does not throw a FileNotFoundException, since as per the docs, FileOutputStream's constructor will create the file if it doesn't exist instead of throwing an error. If you print the error, you see:
java.io.NotSerializableException: TestObject
Again, as per the docs, writeObject requires that its argument be Serializable. Since Serializable is just a marker interface, you can just implement it:
class TestObject implements Serializable
and now your code doesn't throw any errors.
Ok I see now that TestObject obviously needs to implement Serializable
Related
I have a class Book(), Author() and a class CollectionOfBooks()(where I stock all the books in an ArrayList). Then I have my interface where I have a menu where I can add/list/remove/search books. Everything works fine. But, I also want to save my books in a file when I exit the program so when the program ends I call this method(BooksIO() is the class for serialize&deserialize):
public void exitProgram() throws IOException{
System.out.println("Program shuts down, cya!");
BooksIO.outputFile(); // to save my books to the file
}
I am not sure if the books are saved because when I start the program the books don't show up:
public static void main(String[] args) throws IOException, ClassNotFoundException {
UserInterface menu = new UserInterface();
BooksIO.inputFile(); // get the books from the saved file to the library
menu.run();
}
I am not sure what I am doing wrong, can someone help me?
The class for Serialize & DeSerialize :
public class BooksIO {
public static void outputFile() throws IOException{
CollectionOfBooks library = new CollectionOfBooks(); //where the books are saved in an ArrayList
FileOutputStream fout=null;
ObjectOutputStream oos=null;
try{
fout = new FileOutputStream ("stefi.ser");
oos=new ObjectOutputStream(fout);
// I try to save my library to the file
oos.writeObject(library.Books);
System.out.println("Serializing successfully completed");
for(Book c: library.Books){
System.out.println(c.toString());
}
} catch (IOException ex){
System.out.println(ex);
}finally{
try{
if(fout!=null) fout.close();
if(oos!=null) oos.close();
} catch (IOException e){
}
}
}
public static void inputFile() throws IOException, ClassNotFoundException{
CollectionOfBooks library = new//where my books are saved in an ArrayList of type Book CollectionOfBooks();//where my books are saved in an ArrayList of type Book
ObjectInputStream ois = null;
try{
FileInputStream fin = new FileInputStream("stefi.ser");
ois = new ObjectInputStream(fin);
// try to get my books from the file and save it in the library
library.Books = (ArrayList<Book>)ois.readObject();
System.out.println("Deserializing successfully completed");
for(Book c: library.Books){
System.out.println(c.toString());
}
}catch (ClassNotFoundException e){
System.out.println("The class for this type of objects"+
"does not exist in this application!");
throw e;
}finally{
try{
if(ois!=null){
ois.close();
}
}catch (IOException e){
}
}
}
}
Right at the top of your outputFile() method, you are initializing a "library" variable to a new (presumably empty) CollectionOfBooks, and then serializing that.
What you want to do instead is pass the application's instance of CollectionOfBooks into the outputFile() method, and serialize that.
Also, while others may disagree, I find Java serialization to be a bit clunky, and it has some odd edge cases that you need to be aware of. I personally wouldn't use it unless I had to for some reason - I'd use a JSON serialization library, like perhaps GSon, instead.
Just guessing because your question does not include a valid MCVE program yet, but it appears that you're reading in the books but not getting them to where they belong, to the main GUI, quite possibly the UserInterface class (we don't no, since you've not shown us). If so...
Give the UserInterface class a public void updateBooks(CollectionOfBooks books) method, and call this with the updated book collection:
public void updateBooks(CollectionOfBooks collectionOfBooks) {
this.collectionOfBooks = collectionOfBooks;
}
Change inputFile to return a CollectionOfBooks object:
public static CollectionOfBooks inputFile() throws IOException, ClassNotFoundException{
//...
}
and then return the collection.
Then in main you could do:
public static void main(String[] args) throws IOException, ClassNotFoundException {
UserInterface menu = new UserInterface();
menu.updateBooks(BooksIO.inputFile());
menu.run();
}
I'm a Pythonista moving into Java/Scala, and I am wondering how to handle the case where you want an exception to be thrown if it occurs. Take the following toy example:
public class PersonSaver {
private final File file;
public PersonSaver(File file) {
this.file = file;
}
public void save(List<Person> people) {
try (ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(file))) {
output.writeObject(people);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In Python I would want this to throw an error if the file isn't found, and let the calling code handle the exception. Is it convention just to re-throw the same exception?
You can make your method throw those exceptions :
public class PersonSaver {
private final File file;
public PersonSaver(File file) {
this.file = file;
}
public void save(List<Person> people) throws FileNotFoundException {
try (ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(file))) {
output.writeObject(people);
} catch (IOException e) {
//handle the exception you want to handle
e.printStackTrace();
}
}
}
Just make sure you declare your method with the throws statement, or your compiler might not like it ;)
You can also go this way (let's call this a semi-exception-handling) :
public class PersonSaver {
private final File file;
public PersonSaver(File file) {
this.file = file;
}
public void save(List<Person> people) throws FileNotFoundException, IOException {
try (ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(file))) {
output.writeObject(people);
} catch (IOException e) {
/*Some code to clear some data or to handle the
exception but still throw an exception higher*/
throw e;
}
}
}
You can just do the following...
public class PersonSaver {
private final File file;
public PersonSaver(File file) {
this.file = file;
}
public void save(List<Person> people) throws FileNotFoundException {
try (ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(file))) {
output.writeObject(people);
} catch (IOException e) {
e.printStackTrace();
}
}
}
In any part of your code you can throw a throwable object, such as an Exception.
You should also state it in the method signature, letting the JVM know you'll handle that Exception in a caller's block.
Example:
public void save(List<Person> people) throws FileNotFoundException{
try (ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(file))) {
output.writeObject(people);
} catch (IOException e) {
e.printStackTrace();
}
}
You need to consider if the calling code actually knows what to do with the specific exception. You have defined an API about saving a collection of Person. The calling code knows only about a Person and has no idea ideally where the save is done.
If you throw a lower level exception about the file not found you are leaking the abstraction and you won't be able to change the implementation easily if the calling code is starting to be aware of where things are saved.
The proper approach would be to throw an "business" exception like PersonNotPersisted or PersonNotSaved since this is something the calling code would understand and avoid the low level IO exceptions to the higher layer
If you declare a method to throws an checkedexception you dont need to catch it or any of it subtypes:
public void save(List<Person> people) throws IOExcetion {
try (ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(file))) {
output.writeObject(people);
}
}
If you want to handle the exception before you can also do like:
public void save(List<Person> people) throws IOException {
try (ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(file))) {
output.writeObject(people);
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}
The keyword 'throw' fires the exception to the caller.
Consider the following code
public class foo {
public static void main(String[] args) {
MyClass mc = new MyClass();
mc.read();
}
}
and
public class MyClass {
private BufferedWriter verb;
private String vFile;
MyClass()
{
try {
verb = new BufferedWriter(new FileWriter(vFile));
} catch(IOException e) {
System.out.println("Internal error1");
System.out.println(e.getMessage());
}
}
public void read()
{
// read a file and create an array
verb.write("Array created"); // ERROR
}
}
As you can see the write is not placed in a try..catch block. I can write a catch for that, but MyClass has many methods and verb.write is used heavily. I also can write public void read() throws IOException to throw the exception to the the caller, main(). Still I have to put mc.read() in a try..catch block. Since MyClass has numerous methods, then I have to put all of them in a catch block in the main().
So, is there a better way to handle that? Is it possible to redirect all exceptions related to verb to the constructor, MyClass() where a try..catch is defined?
One approach is to make your own "safe" wrapper around BufferedWriter (for that matter, any kind of Writer) and handle I/O errors there:
class SafeWriter {
private final Writer writer;
public SafeWriter(Writer writer) {
this.writer = writer;
}
public void write(int n) {
try {
writer.write(n);
} catch (IOException e) {
handleException(e);
}
}
public void write(String s) {
try {
writer.write(s);
} catch (IOException e) {
handleException(e);
}
}
... // Provide wrappers for other methods here
private void handleException(IOException e) {
...
}
}
Now you can use write methods on your new class to handle exceptions in a uniform way inside the class:
private SafeWriter verb;
...
verb = new SafeWriter(new BufferedWriter(new FileWriter(vFile)));
I am not sure why file , BankAccount.ser is empty after successful run of below code. BankAccount.ser file is a class path resource. After successful run of SuccessfulSerializationTestDriver , BankAccount.ser is zero bytes on disk and has no contents.
public class SuccessfulSerializationTestDriver {
public static void main(String[] args) {
long accountNumber=12033456;
String bankName="SBI";
String branch="NOIDA";
SerializableBankAccount sBankAccount = new SerializableBankAccount();
sBankAccount.setAccountNumber(accountNumber);
sBankAccount.setBankName(bankName);
sBankAccount.setBranch(branch);
try(FileOutputStream fileOut =new FileOutputStream("BankAccount.ser")){
ObjectOutputStream out= new ObjectOutputStream(fileOut);
out.writeObject(sBankAccount);
out.flush();
out.close();
System.out.println("Bank Account is successfully serialized");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Serializable class is ,
public class SerializableBankAccount implements Serializable {
private static final long serialVersionUID = 1L;
private long accountNumber;
private String bankName;
private String branch;
public long getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(long accountNumber) {
this.accountNumber = accountNumber;
}
public String getBankName() {
return bankName;
}
public void setBankName(String bankName) {
this.bankName = bankName;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
#Override
public String toString(){
return accountNumber+","+bankName+","+branch;
}
}
EDIT : I wrote deserializer and I am getting object successfully - so it just seems a visibility issue. Somehow file is shown of zero bytes.
public class SuccessfulDeSerializationTestDriver {
public static void main(String[] args) {
SerializableBankAccount sBankAccount = null;
try(FileInputStream fileIn =new FileInputStream("BankAccount.ser")){
ObjectInputStream inStream= new ObjectInputStream(fileIn);
sBankAccount= (SerializableBankAccount) inStream.readObject();
inStream.close();
System.out.println("Successfully Deserialized Object is "+sBankAccount);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Successfully Deserialized Object is 12033456,SBI,NOIDA
If the file you're looking at is zero bytes, but it deserializes successfully, it sounds like the file is being created elsewhere. Perhaps specify the path explicitly when you create the file name for a start. The file with size 0, may be from an older run - delete that on disk, and see if it gets created again.
I am not able to recreate the problem you're having. When I run your code the BankAccount.ser file is created and is not empty. In fact I wrote a deserialization test to see if I could get the object back by reading the file and it works fine.
Here is the deserializing class in case you want it:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
public class DeserializationTestDriver {
public static void main(String[] args) {
try(FileInputStream fileInput =new FileInputStream("BankAccount.ser")){
ObjectInputStream input = new ObjectInputStream(fileInput);
SerializableBankAccount sBankAccount = (SerializableBankAccount) input.readObject();
input.close();
System.out.println("Bank Account is successfully deserialized: "+sBankAccount.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
I also added a toString method to your SerializableBankAccount:
#Override
public String toString() {
return "SerializableBankAccount [accountNumber=" + accountNumber
+ ", bankName=" + bankName + ", branch=" + branch + "]";
}
After running your serialization code and then running the above deserialization I get this output:
Bank Account is successfully deserialized: SerializableBankAccount [accountNumber=12033456, bankName=SBI, branch=NOIDA]
So clearly the code is fine, which means it has to be something to do with the environment. I suggest checking whether you're running the program with correct privileges, permissions, etc. It seems that something external to your code is preventing you from writing to the file. Either that or perhaps you're looking at the wrong file, verify you have the correct path and check the file creation and modification dates.
Is there a way to save the whole console output to a file when multithreading? I'm working with 5 threads. I had this idea that i can put a printstream in the run-method.
example:
public void run() {
try{
PrintStream out = new PrintStream(file);
stopExecute stop = new stopExecute();
Thread t = new Thread(stop);
Scanner in = new Scanner(System.in);
t.start();
while (!in.hasNextLine())
{
classThatUsingMultipleThrads();
System.out.println("Finished");
anotherClassThatUsingThreads();
System.out.println("Finished");
}
System.out.prinln("User stopped the execution");
stop.keepRunning = false;
System.setOut(out);
}
catch(IOException e){System.out.println(e);}
Problem here is that it's only saving the output "User stoped the execution" and everything in the whileloop are not saved. Or the outputstream from other classes.
I've tried to put the
System.setOut(out);
in the while-loop, but didn't help.
Edit: Spell correction
try {
System.setOut(new PrintStream(new File("output-file.txt")));
}
catch (Exception e) {
e.printStackTrace();
}
thanks to: System.out to a file in java
You should probably look into using a logging library such as Log4J. However you could also use something like a TeeOutputStream. This type of output stream writes to 2 other streams when called. A few libraries have great implementations but you can also write one yourself. I whipped this one up real quick.
You could set the output stream for your entire program in your main method to use this TeePrintStream, then all calls to System.out.* will write data to the usual System.out and your FileOutputStream.
Theres also an implementation of the TeePrintStream here http://www.java2s.com/Code/Java/File-Input-Output/TeePrintStreamteesallPrintStreamoperationsintoafileratherliketheUNIXtee1command.htm
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
public class SO34042494 {
public static void main(String[] args) throws FileNotFoundException {
System.setOut(new TeePrintStream(System.out, new FileOutputStream(new File("x:\\output.txt"))));
System.out.println("Check check");
System.out.println("1");
System.out.println(2);
System.out.println(3L);
}
public static class TeePrintStream extends PrintStream {
private final OutputStream tee;
public TeePrintStream(PrintStream original, OutputStream tee) {
super(original);
this.tee = tee;
}
#Override
public void write(byte[] b) throws IOException {
super.write(b);
tee.write(b);
}
#Override
public void write(byte[] buf, int off, int len) {
super.write(buf, off, len);
try {
tee.write(buf, off, len);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
#Override
public void write(int b) {
super.write(b);
try {
tee.write(b);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
#Override
public synchronized void close() {
try {
tee.close();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
super.close();
}
}
}
}
The TeePrintStream I have here is something I just threw together, please if your going to use this in a production project polish it up and test it thoroughly
Okej i think i solved it. In my main i just did like this:
public static void main(String[] args) {
Prinstream out = new Prinststream(file);
/*
Do some things like start threads, call objects etc..
.
.
.
.
*/
System.setOut(out);
I think that when all threads are started and doing their things(i do assign a object to each thread) the printstream will catch every console-output that occurs in other classes.
This didn't work before i edited my stopExecute class.
public class stopExecute implements Runnable {
Scanner scan = new Scanner(System.in);
private Object[] obj;
public stopExecute(Object[] obj)
{
this.obj = obj;
}
public void run() {
while(true){
stop();
}
}
public void stop() {
if(scan.hasNextLine()){
System.out.println("Stopped");
System.exit(0);
}
}
}
Thank you for your help guys. I will look into your suggestions and try them. In this solution i'm unable to use Log4J. But i will definitely check it out.