简体   繁体   中英

What is the difference between ObjectInputStreamReader and InputStreamReader?

My teacher said that in file server program ObjectInputStreamReader is compulsory to write. When I asked the reason then he said me it is comfortable for file server program. I am thinking that it is not necessary reason. Why InputStreamReader or other alternatives can not be used? what is the advantage of ObjectInputStreamReader over InputStreamReader .

Here code for client/server:

public class Client {
    public static void main(String[] args) {
        Socket s = null;
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;
        Scanner sc = new Scanner(System.in);

        String data = "";
        try {
            s = new Socket("localhost", 1234);
            System.out.println("client is connectd");

            ois = new ObjectInputStream(s.getInputStream());
            String jai = (String) ois.readObject();
            System.out.println("DATA from SERVER:" + jai);
            oos = new ObjectOutputStream(s.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("Enter file name:");
        try {
            String fil = (String) sc.next();
            OutputStream pw = new FileOutputStream(fil + ".new");
            oos.writeObject(fil);
            data = (String) ois.readObject();
            pw.write(data.getBytes());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        System.out.println("Content of file:" + data);
    }
}

Can any one say what is actual reason ?

I think you mean ObjectInputStream and BufferedInputStream (not readers).

ObjectInputStream wraps input stream and provides typed methods that allow reading data of certain type from the stream. For example readDouble() , readObject() etc.

BufferedInputStream does not provide additional API (comparing to regular InputStream ). The only thing it does is buffering of data, ie it reads data chunk-by-chunk that is much more efficient way than reading byte-by-byte.

An InputStream is an abstract class that can be used to define any type of input stream, including reading from file systems, URLs, sockets, etc.

You don't actually create an InputStream , as it doesn't mean anything by itself. Rather, you create a type of InputStream that defines how to read/write a particular type of data, such as the suggested ObjectInputStream . This class defines that the data being written is a Java Object (that implements Serializable or Externalizable ). There are other InputStreams that are used for generic file data, images, audio, and a whole range of other types.

There is no such thing as an ObjectInputStreamReader , unless you write a class like this yourself that has the purpose of writing to an ObjectInputStream .

Refer to the ObjectInputStream and InputStream Java docs for more enlightenment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM