【java】io流之字节输入流:java.io.InputStream类及子类java.io.FileInputStream

时间:2021-09-09 04:26:48
 package 文件操作;

 import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream; public class TestInputStream {
public static void main(String[] args) throws IOException {
File file=new File("D:"+File.separator+"test.txt"); /*一次性读取文件内容
if(file.exists()){
InputStream inputStream=new FileInputStream(file);
byte[] bytes=new byte[1024];
int length=inputStream.read(bytes);
inputStream.close();
System.out.println("【"+new String(bytes,0,length)+"】");
}
*/ if(file.exists()){//单个读取
InputStream inputStream=new FileInputStream(file);
byte[] bytes=new byte[1024];
int foot=0;
byte tmp=0;
while((tmp=(byte)inputStream.read())!=-1)
bytes[foot++]=tmp;
inputStream.close();
System.out.println("【"+new String(bytes,0,foot)+"】");
}
}
}