java - 对Stream读写时要先后用到两种读取方式怎么办?
迷茫
迷茫 2017-04-17 13:02:27
0
1
363

有关java.net.Socket和java.io
server要传输数据给client,想先用ObjectOutputStream的方式读写一个对象,之后改用BufferedWriter读写。client处用对应的相反方式。

可我要不只能用ObjectOutputStream读写,要不只能用BufferedWriter读写。应该怎么办?

server处大概是这样的:

java            oos = new ObjectOutputStream(client.getOutputStream());
            oos.writeObject(dc);
//          oos.close();

            bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));          

            bw.write("hahaha");
            bw.flush();

            while(true){
                line=br.readLine();
                if(line != null){
                    ExecuteInput(line);
                }
            }   

client处大概是这样的:

java            ois = new ObjectInputStream(socket.getInputStream());
            XXX dc = (XXX) ois.readObject();
//          ois.close();


        String line;
        while(true){
            try {
//              System.out.println("000000");
                line=br.readLine();
//              System.out.println("111111");
                if(line != null)
                    System.out.println("aaaa "+line);
            } catch (IOException e) {
                System.out.println("222222");
                e.printStackTrace();
            }
        }

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(1)
大家讲道理

Seeing no one answered, I asked a question on stackoverflow (http://stackoverflow.com/questions/27737268/java-sockets-read-write-stream-in-two-different-ways-one-by- one)
Their answer was that ObjectOutputStream and BufferedWrite cannot be mixed, only one of them can be used.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template