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

看板java作者 (......)時間19年前 (2006/06/18 02:12), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/3 (看更多)
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: 140.113.122.119 ※ 編輯: KOCN 來自: 140.113.122.119 (06/18 02:31)
文章代碼(AID): #14b4Oboz (java)
文章代碼(AID): #14b4Oboz (java)