作者alen127 (小印)
看板java
标题Re: [问题] 请问readbyte()跟read() 有什麽不同?
时间Sun Jun 18 04:37:53 2006
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