[英]file input stream odd behaviour
我有以下簡單的代碼,該代碼從當前目錄中的文件讀取到字節數組中,並打印該數組的內容(這是文件的內容,ASCII可打印字符從ASCII 32到ASCII 126):
import java.io.FileInputStream;
import java.io.IOException;
class Input {
public static void main(String[] args) throws IOException {
FileInputStream fis=null;
try {
fis=new FileInputStream("file.txt");
int available=fis.available();
byte[] read=new byte[available];
int bytesRead;
int offset=0;
while(offset<read.length) {
bytesRead=fis.read(read,offset,read.length-offset);
if (bytesRead==-1) break;
offset+=bytesRead;
}
System.out.println(read.length);
for (byte b:read) {
int i=b & 0xFF;
System.out.write(i);
System.out.write('\t');
}
}
finally {
if (fis != null)
try {
fis.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
}
但是,當它運行時,它僅將64個字符輸出到標准輸出中(即使調試字符串按應有的方式在數組中輸出了96個字節,也是如此)。我不知道我在做什么錯。
您需要flush()
System.out
,因為只有設置了autoFlush
,它才會在\\n
上刷新(默認)。 請參閱文檔PrintStream和該選項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.