作者KOCN (......)
看板java
标题[问题] 请问readbyte()跟read() 有什麽不同?
时间Sun Jun 18 02:28:49 2006
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)