Re: [問題] 請問readbyte()跟read() 有什麼不同?

看板java作者 (小印)時間19年前 (2006/06/18 04:31), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/3 (看更多)
read() 它是用來讀出資料中單一的無號位元組,傳回無號位元組的整數值 也就是說它所傳回的值只在0~255之間 至於readByte() 它也是讀取單一位元組,但是不同餘的部分就在於它所回傳的值卻是在 -128~127之間的有號byte() 也就是這兩個不同地方就在於 一個回傳值是0~255 另一個是-128~127 這應該是最大的不同點吧,以上是個人知道的部分假如在下有所錯誤 請麻煩糾正一下 因為有糾正才會進步...多謝!! ※ 引述《KOCN (......)》之銘言: : readByte() : reads an 8 bit byte : 在DataInputStream裡面 : read() : reads a byte of data : 在FileInputStream裡面 : 一個是傳回byte一個是傳回int : 這是老師上課給的範例 : import java.io.*; : public class dump { // 此例中 DataInputStream 其實是不需要的 .. : static final String FNAME = "aaa.dat"; : static String fileName = FNAME; : public static void main(String arg[ ]) { : BufferedReader br = null; : FileInputStream fis = null; : DataInputStream dis = null; // binary input if necessary : try { : System.out.print("Dump which file? "); : br = new BufferedReader( : new InputStreamReader(System.in)); : fileName = br.readLine( ); br.close( ); : if(fileName.length()==0) fileName = FNAME; : fis = new FileInputStream(fileName); //read byte as int : dis = new DataInputStream( fis ); : } catch (Exception e) { : System.out.println("Error open file " + fileName); : System.exit(49); : } : int n, c; // read( ) in InputStream returns int : n = 0; // 用起來與 C 語言的 fgetc(fp) 比較像 : try { : c = fis.read( ); // 在此例中, 若用 dis.read( ) 也可以 : while( c != -1) { // 讀取到 -1 表示 EOF : if(c < 16) System.out.print("0"); // ensure 2 hexdigits : System.out.print(Integer.toHexString(c) + " "); : ++n; : if(n%16 == 0) System.out.print("\n"); : else if(n%8 == 0) System.out.print(" : "); : c = fis.read( ); // : // : //如果我整個程式改用dis來輸入 : //這裡改成c=dis.readByte()的話 可以嗎? : // : } // while : } catch (Exception e) { } : System.out.print("\nTotal "+ n + " chars.\n" ); : } // main : } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 211.74.248.89
文章代碼(AID): #14b6QlGU (java)
討論串 (同標題文章)
文章代碼(AID): #14b6QlGU (java)