字节流读取过程In
--1,I/in
--读取的过程 : 从磁盘 读到 程序里
--根据读取数据的单位不同 可以分为 字节流和字符流
--字节流读啥文件都可以,字符流只能读txt文件
--2,读取工具类
--InputStream
--是父类,而且被修饰为了 抽象类 ,不能new
--学习共性方法
abstract int read()
从输入流中读取数据的下一个字节。
int read(byte[] b)
从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。
int read(byte[] b, int off, int len)
将输入流中最多 len 个数据字节读入 byte 数组。
void close()
关闭此输入流并释放与该流关联的所有系统资源。
--FileInputStream
--是子类,方法都是继承来的,研究创建对象
--FileInputStream 从文件系统中的某个文件中获得输入字节。
--创建对象
FileInputStream(String name)
FileInputStream(File file)
--BufferedInputStream
--子类,方法都是继承来的,主要学习创建对象
--BufferedInputStream 为另一个输入流添加一些功能,即缓冲的能力。在创建 BufferedInputStream 时,会创建一个内部缓冲区数组。
--创建对象
BufferedInputStream(InputStream in)
import java.io.*;
//测试 字节读取流
//总结:
//1, 字节流读取 : FileInputStream / BufferedInputStream
//2, 效率上来讲: BufferedInputStream > FileInputStream
//3, 原因是: BufferedInputStream 底层维护了一个byte[] buf ,
//当数组满了之后才一次性从流里读进来 ,默认的数组容量是8192字节相当于8K
//4, 读取想要高效,一般使用BufferedInputStream . 也叫缓冲流/高级流
public class Test3_in {
public static void main(String[] args) throws IOException {
// method();//FileInputStream读取
method2();//BufferedInputStream读取
}
//BufferedInputStream读取 -- 高效/缓冲流/高级流
public static void method2() throws IOException {
//1,创建多态对象
// BufferedInputStream(InputStream in)
InputStream in = new BufferedInputStream(
new FileInputStream("D:\\iotest\\1.txt") );
//2,开始读取
int b = 0;//定义变量,记录读到的数据
while( ( b = in.read() ) != -1){//没数据就会读到-1,循环
System.out.println(b);//打印读到的数据
}
//3,释放资源
in.close();
}
//FileInputStream读取
public static void method() throws IOException {
//1,创建多态对象
// FileInputStream(String name)
// FileInputStream(File file)
InputStream in = new FileInputStream("D:\\iotest\\1.txt");
// InputStream in2 = new FileInputStream(new File("D:\\iotest\\1.txt") );
//2,读取数据
// TODO read()从读取流中一个一个字节的读取
// int data = in.read();
// System.out.println(data);
// int data2 = in.read();
// System.out.println(data2);
// int data3 = in.read();
// System.out.println(data3);
// //TODO 没数据的话,会读到什么呢? -- 永远是-1
// int data6 = in.read();
// System.out.println(data6);
// int data7 = in.read();
// System.out.println(data7);
//TODO 改造读取的过程
int b = 0 ; //定义变量,记录读到的数据
while ( ( b = in.read() ) != -1 ){//如果没数据了就会读到-1,就会结束循环
System.out.println(b);//打印读到的数据
}
//3,释放资源
in.close();
//TODO 当资源被释放后,不能再读了,否则抛出异常:Stream Closed
// int data7 = in.read();
// System.out.println(data7);
}
}
字节流写出过程Out
--1,O/Out
-写出的过程 : 从程序 写出到 磁盘里
--根据写出数据的单位不同 可以分为 字节流和字符流
--字节流写啥数据都可以,字符流只能写出成txt文件
--2,写出工具类
--OutputStream
--是父类,,而且被修饰成了抽象类, 不能new
--共性方法
void close()
关闭此输出流并释放与此流有关的所有系统资源。
void flush()
刷新此输出流并强制写出所有缓冲的输出字节。
void write(byte[] b)
将 b.length 个字节从指定的 byte 数组写入此输出流。
void write(byte[] b, int off, int len)
将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。
abstract void write(int b)
将指定的字节写入此输出流。
--FileOutputStream
--子类,方法可以继承父类的,,,,学习创建对象
--文件输出流是用于将数据写入 File 的输出流
--创建对象
FileOutputStream(File file)
FileOutputStream(String name)
FileOutputStream(File file, boolean append)
FileOutputStream(String name, boolean append)
--BufferedOutputStream
--子类,,可以使用继承来的方法,研究创建对象
--该类实现缓冲的输出流。
--创建对象
BufferedOutputStream(OutputStream out)
--3,测试
package cn.tedu.io;
import java.io.*;
//测试 字节读写出
//总结:
//1,字节流写出的工具类: FileOutputStream / BufferedOutputStream(高级流/缓冲流)
//2,效率上来讲: BufferedOutputStream > FileOutputStream
//3,原因是: BufferedOutputStream 底层维护了一个byte[] buf用来缓冲数据,
// 当数组满了 就把数据一次性保存磁盘里 . 默认的数组容量是8192字节相当于8K
public class Test4_out {
public static void main(String[] args) throws IOException {
// method();// FileOutputStream写出
method2();// BufferedOutputStream写出
}
// BufferedOutputStream写出
public static void method2() throws IOException {
//1,创建对象
// BufferedOutputStream(OutputStream out)
OutputStream out = new BufferedOutputStream (
new FileOutputStream("D:\\iotest\\2.txt") ) ;
OutputStream out2 = new FileOutputStream("D:\\iotest\\2.txt");
//2,开始写出
out.write(48);
out.write(49);
out.write(50);
//3,释放资源
// out.flush();//把数据从out流里刷出去,刷到磁盘保存
out.close();//flush + close
}
// FileOutputStream写出
public static void method() throws IOException {
//1,创建对象
// FileOutputStream(File file)
// OutputStream out = new FileOutputStream(new File(""));
// FileOutputStream(String name)
//TODO 创建FileOutputStream对象时,只传1个参数,默认就是数据覆盖
// OutputStream out = new FileOutputStream("D:\\iotest\\1.txt");
//TODO 想实现数据追加,就需要传入第二个参数true,表示要改成数据追加
OutputStream out = new FileOutputStream("D:\\iotest\\2.txt",true);
//2,开始写出
out.write(97);
out.write(97);
out.write(97);
out.write(97);
//3,释放资源
out.close();
//TODO 流关了还能继续写出吗? --不可以,否则抛出异常:Stream Closed
// out.write(100);
}
}
改造close()
为了资源一定会被释放,要求close()必须要执行,有没有异常都必须执行的代码,,放在finally里
public class Test3_in {
public static void main(String[] args) throws IOException {
// method();//FileInputStream读取
method2();//BufferedInputStream读取
}
//BufferedInputStream读取 -- 高效/缓冲流/高级流
public static void method2() {
InputStream in = null;
try{
//1,创建多态对象 // BufferedInputStream(InputStream in)
in = new BufferedInputStream(
new FileInputStream("D:\\iotest\\1.txt") );
//2,开始读取
int b = 0;//定义变量,记录读到的数据
while( ( b = in.read() ) != -1){//没数据就会读到-1,循环
System.out.println(b);//打印读到的数据
}
}catch(Exception e){
e.printStackTrace();
}finally {//3,释放资源 --为了 代码一定会被执行,放finally里
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用try-catch处理问题,将.close()放到finally中,保证报错后还能执行代码,然后再次捕获异常