有关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(); } }
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.