Re: [問題] socket傳送數值出錯
: rbw.write(controla[i+1]);
: gui.border=br.read();
: → andykid:我的值都可以正常讀入..除了height..每次都是63 01/17 12:56
你誤會了 write(int) 和 int read() 的用法,它們是用來寫/讀 character
而不是 integer 的,請看以下:
import java.io.*;
public class Test {
public static void main(String[] argv) throws Exception {
PipedOutputStream o = new java.io.PipedOutputStream();
PipedInputStream i = new java.io.PipedInputStream(o);
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(o));
w.write(100000);
w.flush();
o.flush();
w.close();
o.close();
System.out.println("read: " + i.read());
}
}
結果出來仍然會是 63, 所有 write(int b) 中, b 值比 127 大的話,就會變成 63 的了
註: charset 問題,當 OutputStreamWriter 設成 charset = UTF-8,比 65535 大的數
便會當成 0
附帶一提,正常來說玩這種 primitive data transfer,java.io.DataInputStream 和
java.io.DataOutputStream 是好物
--
勁過呂布的勁過相簿...
http://www.pixnet.net/superlubu
亂七八糟的,不好意思 m(_ _)m
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 147.8.130.225
※ 編輯: superlubu 來自: 147.8.130.225 (01/17 13:29)
※ 編輯: superlubu 來自: 147.8.130.225 (01/17 16:30)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 10 篇):