> 技术文档 > IO流的介绍

IO流的介绍


概述:

I:input;O:output

File:表示系统中的文件或者文件夹路径(File类只能对文件本身操作,不能读写文件里面储存的数据

IO流:用于读写文件中的数据(可以读写文件或网络中的数据)

IOl流的分类:

  • 流的方向:IO流分为输入流(读取)和输出流(写出
  • 操作文件类型:IO流分为字节流(所有类型的文件)和字符流(纯文本文件
  • 纯文本文件:Windows自带的记事本能打开读懂

IO流体系:

字节输出流演示:

package www.IOClass.com;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class IOclass { public static void main(String[] args) throws IOException { /** * 演示:字节输出流FileOutPutStream * 实现需求:写出一段文字到本地文件中 * * 实现步骤: * 1.创建对象 * 细节1.参数是字符串表示的路径或者File对象都是可以的· * 细节2.如果文件不存在,但是要保证父级路径是存在的 * 细节3.如果文件已经存在,则会清空文件夹 * 2.写出数据 *细节2.write方法的参数是整数,但实际上写道本地文件中的是ASCII上对应的字符 * 3.释放资源 *每次使用完流之后都要释放资源 * */ //创建对象, // 写出输出流OutPutStream, // 本地文件:File FileOutputStream fos = new FileOutputStream(\"F:\\\\develop\\\\idea-code\\\\IOClass\\\\b.txt\"); //2.写出数据 fos.write(97); //释放资源 fos.close(); }}

FileOutputStream

写入数据的三种方式

方法名称 说明 void write(int b) 一次写一个字节数据 void write(byte[] b) 一次写一个字节数组数据 void write(byte[] b, int off, int len) 一次写一个字节数组的部分数据
package www.IOClass.com;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class IOclss1 { public static void main(String[] args) throws IOException {// 方法名称说明// void write(int b)一次写一个字节数据// void write(byte[] b)一次写一个字节数组数据// void write(byte[] b, int off, int len)一次写一个字节数组的部分数据 //参数一:数组,参数二:起始索引;参数三:个数 //1.创建对象 FileOutputStream fos =new FileOutputStream(\"F:\\\\develop\\\\idea-code\\\\IOClass\\\\a.txt\"); //2.写出数据 // void write(int b)一次写一个字节数据 fos.write(97); fos.write(98); // void write(byte[] b)一次写一个字节数组数据 byte[] bytes={97,98,99,100}; fos.write(bytes); fos.write(bytes,1,2); //释放资源 fos.close(); }}

细节:

package www.IOClass.com;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class IOclass2 { public static void main(String[] args) throws IOException { /** * 换行写 * 再写出一个换行符就行了 * window:\\r\\n * Linux:\\n * Mac:\\r * * 细节: * 在window操作系统中,java对回车换行进行了优化 * 虽然完整的十四\\r\\n,但是我们写出其中一个, * java也会换行因为java会自动补全 * * * 续写 *如果想要续写的开续写开关就好 * 默认false,表示关闭 * 如果打开,此时创建对象,不会清空文件 * * * */ FileOutputStream fos = new FileOutputStream(\"F:\\\\develop\\\\idea-code\\\\IOClass\\\\b.txt\",false);//续写开关本是关闭的 //写出数据 String str = \"大梦谁先觉\"; byte[] bytes = str.getBytes(); fos.write(bytes); String str2 = \"\\r\";//换行 fos.write(str2.getBytes()); String str3 = \"平生我自知\"; fos.write(str3.getBytes()); //释放资源 fos.close(); }}

FileInputStream:

操作本地文件的字节输入流,可以把本地文件中的数据读取到程序中来

书写步骤:

  1. 创建字节输入流对象
  2. 读取数据
  3. 释放资源

示例:

package www.IOClass.com;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class IOclass3 { public static void main(String[] args) throws IOException {// 书写步骤://// 创建字节输入流对象// 读取数据// 释放资源 //文件中的数会被以ASCLL码的形式返回 FileInputStream fis =new FileInputStream(\"F:\\\\develop\\\\idea-code\\\\IOClass\\\\a.txt\"); //数据是一个一个读出来的,一次一个// int a=fis.read();//// System.out.println(a);// int b=fis.read();//// System.out.println(b);// int c=fis.read();//// System.out.println(c);// int d=fis.read();//// System.out.println(d);// int e=fis.read(); // System.out.println(e); //释放资源 fis.close(); }}

FileInputStream书写细节和循环读取:

package www.IOClass.com;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class IOclass3 { public static void main(String[] args) throws IOException {// 书写步骤://// 1.创建字节输入流对象// 细节1:如果文件不存在,直接报错// 2.读取数据//  细节2一次只读取一个字节,读出来的是数据在ASCLL上对应的数据// 细节3:读到文件末尾了,read方法返回-1// 3.释放资源// 每次用完都要释放 //文件中的数会被以ASCLL码的形式返回 //循环输入 FileInputStream fis = new FileInputStream(new File(\"F:\\\\develop\\\\idea-code\\\\IOClass\\\\a.txt\")); int a; //循环读取 while((a=fis.read())!=-1) { System.out.print((char)a); } fis.close(); }}

文件拷贝的基本代码

package www.IOClass.com;import java.io.*;public class IOclass4 { public static void main(String[] args) throws IOException { /** * 练习: * 文件拷贝: * * */ FileOutputStream fos = new FileOutputStream(\"F:\\\\develop\\\\idea-code\\\\IOClass\\\\a.txt\"); FileInputStream fis = new FileInputStream(\"F:\\\\develop\\\\idea-code\\\\IOClass\\\\b.txt\"); int a; while((a=fis.read())!=-1) { fos.write(a); } fis.close(); fos.close(); }}

弊端:这样写大家肯定发现,一次只读取了一个字节的弊端所以下面我来讲一次性读取多个字符的方法:

方法名称 说明 public int read() 一次读一个字节数据 public int read(byte[] buffer) 一次读一个字节数组数据

下面是演示示例:

package www.IOClass.com;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class IOclass5 { public static void main(String[] args) throws IOException {// 方法名称说明// public int read()一次读一个字节数据// public int read(byte[] buffer)一次读一个字节数组数据// 创建数组的时候尽量用1024的整数倍// public int read(byte[] buffer)一次读一个字节数组数据//示例1:// 示例: FileInputStream fis = new FileInputStream(\"F:\\\\develop\\\\idea-code\\\\IOClass\\\\a.txt\"); byte[] bytes = new byte[2]; int len=fis.read(bytes);//返回的是读取了几个数据.具体读取多少,要看数组长度 System.out.println(len); //在这里会发现,一次不能能读取完,他的作用机制就是覆盖,如果文件中有abcde,但是byte数组长度是2,第一次是ab第二次是cd,第三次是ed //在这里肯定有人疑问为什么第三次是ed。 //原理就是他的机制是覆盖,比如第一次是ab,第二次的c就会覆盖a,d就会覆盖b,所以最后ed的原因就是e覆盖了c,d没有被覆盖。// 如果最后读不到数据就返回-1 System.out.println(new String(bytes,0,len)); fis.close(); }}

复制文件示例:

package www.IOClass.com;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class IOclass6 { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream(\"E:\\\\屏幕截图\\\\WeChat_20250529214652.mp4\"); FileOutputStream fos = new FileOutputStream(\"F:\\\\develop\\\\idea-code\\\\IOClass\\\\老婆.mp4\"); byte[] buffer = new byte[1024]; int len = 0; while ((len=fis.read(buffer))!=-1) { fos.write(buffer,0,len); } fis.close(); fos.close(); }}

trt-catch异常处理 

        正常抛出异常可能这样写,不过可能执行不到fos.close(),
所以try-catch语句有一个finally语句,意思就是不管上面如何,finally之中的都会执行
不过如果把fos.close放到finally中,会因为fos对象不在当前括号内,调用不到
方法一,在外面声明两个对象,但是不new空间.

package www.IOClass.com;import java.io.FileInputStream;import java.io.FileOutputStream;public class IOclass7 { public static void main(String[] args) { //正常抛出异常可能这样写,不过可能执行不到fos.close(), //所以try-catch语句有一个finally语句,意思就是不管上面如何,finally之中的都会执行 //不过如果把fos.close放到finally中,会因为fos对象不在当前括号内,调用不到 //方法一,在外面声明两个对象,但是不new空间 FileOutputStream fos=null; try { fos = new FileOutputStream(\"F:\\\\develop\\\\idea-code\\\\IOClass\\\\b.txt\"); fos.write(97);// fos.close(); } catch (Exception e) { e.printStackTrace(); } finally{ if(fos!=null) { try {  fos.close(); } catch (Exception e) {  e.printStackTrace(); } } } }}

有三种方式 

 第二种方案:是由实现了AutoCloseable接口的类,才能在小括号中创建对象