IO_文件字节输出流
每字节获取
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/*
* 1.创建源 选择房子
* 2.选择流 选择搬家公司
* 3.操作 选择用什么搬
* 4.释放资源 打发搬家公司
*/
public class IOtest01 {
public static void main(String[] args) {
//1.创建源
File src = new File("D:/WorkSpace/IO/src/io.txt");
InputStream is = null;
//2.选择流
try {
is = new FileInputStream(src);
//3.操作(读取)
int temp;
try {
while ((temp=is.read())!=-1) {//结尾的结果等于-1
System.out.print((char)temp);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//4.释放资源
try {
if (null!=is) { //如果 is没有就不存在释放
is.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
分段获取
把第三步操作替换
//3.操作(分段读取)
byte[] flush = new byte[1024];//缓冲容器
int len = -1; //接收长度
try {
while ((len=is.read(flush))!=-1) {//结尾的结果等于-1
//字节数组-->字符串(解码)
String temp = new String(flush, 0, len);
System.out.print(temp);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}