1. What will happen when you attempt to run the following code? import java.io.*
ID: 3560732 • Letter: 1
Question
1. What will happen when you attempt to run the following code?
import java.io.*;
public class Test {
public static void main(String[] args) throws IOException {
ObjectOutputStream output =
new ObjectOutputStream(new FileOutputStream("object.dat"));
output.writeObject(new A());
}
}
class A implements Serializable {
B b = new B();
}
class B {
}
2. Is it true that DataInputStream/DataOutputStream can always be replaced
by ObjectInputStream/ObjectOutputStream
Explanation / Answer
1) b. Compile time error, the second for statement is malformed
2) yes
IOUtils.toString(InputStream) must read the stream until its end, which would imply that the peer has disconnected. So you can't write to it.
If you're exchanging Strings with data streams you should use writeUTF() and readUTF().
Or read and write lines, with a BufferedReader/Writer.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.