流 stream
根据方向:输入流、输出流
根据操作单位:字节流 字符流
组合后共有四种:
字节输入流 、字节输出流
字符输入流 、字符输出流
学习抽象父级的公共方法,学习子类的创建方式;
字节输入流:InputStream 抽象父类--不能实例化
:FileInputStream 文件字节输入流,构造函数参数:File/String
FIS in = new FIS(new File(路径));(进行了缩写,下同)
FIS in = new FIS(路径);
private static void method1(){ //使用普通字节流读取 //InPutStream抽象父类,不可实例化 //FileInputStream流对象,参数为流对象操作的文件对象,或者直接文件路径 //InputStream in = new FileInputStream("D:\\ready\\1.txt"); InputStream in = null; //要初始化 try { in = new FileInputStream(new File("D:\\ready\\1.txt"));//文件夹找不到异常 //in = new FileInputStream("D:\\ready\\1.txt"); //使用字节输入流对象进行读取 //read()每次调用只会读取一个数据字节 // System.out.println(in.read()); // System.out.println(in.read()); // System.out.println(in.read()); // System.out.println(in.read());//读到末尾返回-1,表示文件结束 int b;//保存每次读取的返回值类型 while((b=in.read())!=-1){ //IOException System.out.println(b); } //while(in.read()!=-1){ // System.out.println(in,read()); // }错误会跳着读,因为每次调用一次in.read()调用的就是下一个字符 in.close(); //关闭流资源 } catch (Exception e) { e.printStackTrace(); }finally { //不管有没有异常,一定会执行的代码块 //所以常常用来执行流关闭操作 try { in.close(); //有可能出现IOException } catch (IOException e) { e.printStackTrace(); } } }
:BufferedInputStream高效字节输入流
BIS in = new BIS(new FIS(new File(路径)));
BIS in = new BIS(new FIS(路径));
private static void method2(){ //使用高效字节流输入 InputStream in=null; try { in = new BufferedInputStream(new FileInputStream(new File("D:\\ready\\1.txt"))); //new BufferedInputStream(new FileInputStream(name)); int b; while((b=in.read())!=-1){ System.out.println(b); } } catch (Exception e) { e.printStackTrace(); }finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } }
字节输出流:OutputStream 抽象父类,不能实例化
:FileOutputStream 文件字节输出流
FOS out = new FOS(new File(路径),append); FOS out = new FOS(路径,append);
private static void method1() { OutputStream out = null; try{ //FOS的构造函数有一个布尔参数append,默认为false,表示覆盖 //设置为true,那么每次输出的内容会追加在原来文件内容之后,不覆盖了 out= new FileOutputStream("D:\\ready\\1.txt"); out.write(97); }catch (Exception e){ e.printStackTrace(); }finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } }
:BufferedOutputStream 高效字节输出流
BOS out = new BOS (new FOS(new File(路径),append)); BOS out = new BOS(new FOS(路径,append));
private static void method2() { OutputStream out =null; try{ out= new BufferedOutputStream(new FileOutputStream("D:\\ready\\1.txt")); out.write(98); }catch (Exception e){ e.printStackTrace(); }finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } }
字符输入流:Reader 抽象父类 不能实例化
: FileReader文件字符输入流
FR in = new FR(new File(路径));
FR in = new FR(路径);
private static void method1() { /*1.创建流对象 使用流对象 关流 */ Reader in = null; try { //Reader in2 = new FileReader("D:\\ready\\1.txt"); in = new FileReader(new File("D:\\ready\\1.txt")); int b; while((b=in.read())!=-1){ System.out.println(b); } } catch (Exception e) { e.printStackTrace(); }finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } }
:BufferedReader高效字符输入流
BR in = new BR(new FR( new File(路径)));
BR in = new BR(new FR(路径));
private static void method2() { Reader in = null; try{ in = new BufferedReader(new FileReader("D:\\ready\\1.txt")); int b; while((b=in.read())!=-1){ System.out.println(b); } }catch (Exception e){ e.printStackTrace(); }finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } }
字符输出流:Writer 抽象父类 不能实例化
:FileWriter 文件字符输出流
FW out = new FW(new File(路径),append); FW out = new FW(路径,append);
private static void method1() { Writer out=null; try{ out = new FileWriter("D:\\ready\\1.txt",true); out.write(98); }catch (Exception e){ e.printStackTrace(); }finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } }
:BufferedWriter 高效字符输出流
BW out = new BW(new FW(new File(路径),append)); BW out = new BW(new FW(路径,append));
private static void method2() { Writer out=null; try{ out = new BufferedWriter(new FileWriter("D:\\ready\\1.txt",true)); out.write(97); } catch (Exception e){ e.printStackTrace(); }finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } }