IO流概述
从程序发送数据到 文件 是输出流
程序 从文件 读取数据叫 输入流
直接操作 文件 读取数据 叫 节点流
对已有流 的操作 叫 处理流 (比如加快传输速度)
流的 单位为 byte 叫字节流
流的 单位为 char 叫字符流
/* 一、 流的分类 1. 操作数据单位: 字节流、字符流 2. 数据的流向:输入流、输出流 3. 流的角色: 节点流、处理流 二、 流的体系结构 抽象基类 节点流(或文件流) 缓冲流(处理流的一种) InputStream FileInputStream (read(byte[] buffer)) BufferedInputStream (read(byte[] buffer)) OutputStream FileOutputStream (writer(byte[] buffer,0,1en)) BufferedOutputStream (writer(byte[] buffer,0,1en)) Reader FileReader (read(char[] charBuffer)) BufferedReader(read(char[] charBuffer)) Writer FileWriter (writer(char[] charBuffer,0,len)) BufferedWriter(writer(char[] charBuffer,0,len)) */
节点流
/*节点流:FileReader FileWriter FileInputStream FileOutputStream*//* 将IO流下的hello.txt文件内容都入程序中,并输出到控制台 说明:1. read()理解:返回读入的一个字符。如果达到文件末尾,返回-1 2. 异常的处理:为了保证流资源一定可以执行关闭操作。需要使用 try-catce-finally处理关闭资源。 3. 读入的文件一定要存在,否则就会报FileNotFoundException. */ @Test public void testFileReader() throws IOException { // 1.实例化File类的对象,指明要操作 的文件 File file = new File("hello.txt");// 相对路径 :相较于当前模块(当前项目) 下 // 2.提供具体的流 FileReader fr = new FileReader(file); // 3.数据的读入过程 int data = fr.read(); while (data != -1){ System.out.print((char) data); data = fr.read(); } /* 语法上的修改 int data; while ((data = fr.read()) != -1){ System.out.print((char) data); } */ // 4.流的关闭操作 fr.close(); } @Test public void testFileReader1() throws IOException { FileReader fr = null; try { // 1.实例化File类的对象,指明要操作 的文件 File file = new File("hello.txt");// 相对路径 :相较于当前模块(当前项目) 下 // 2.提供具体的流 fr = new FileReader(file); // 3.数据的读入过程 int data = fr.read(); // 空参 一次读一个字符 while (data != -1){ System.out.print((char) data); data = fr.read(); } } catch (IOException e) { e.printStackTrace(); } finally { // 4.流的关闭操作 try { if (fr != null){ fr.close(); } } catch (IOException e) { e.printStackTrace(); } /* 或 if(fr != null){ try{ fr.close(); }catch (IOException e){ e.printStackTrace(); } } */ } } // 对read()操作升级:使用read的重载方法 @Test public void testFileReader2() { FileReader fileReader = null; try { // 1.File类的实例化 File file = new File("hello.txt"); // 2.FileReader流的实例化 fileReader = new FileReader(file); // 3.读入的操作 // read(char[] cbuf):返回每次读入cbuf数组中的字符的个数。如果达到文件末尾,返回-1 char[] cbuf = new char[5]; int len; while ((len = fileReader.read(cbuf)) != -1){ /* 错误写法 for (int i = 0;i < cbuf.length; i ++){ // cbuf.length是 char数组的长度 len 是读出来返回的长度 System.out.print(cbuf[i]);// helloworld123ld 123 去覆盖 world 只有前三个覆盖了 } 错误写法 String str = new String(cbuf); System.out.print(str); 正确写法 String str = new String(cbuf,0,len); // 该构造器 只转换 0~len之间的 左闭右开 System.out.pring(str); */ for (int i = 0; i < len; i ++){ System.out.print(cbuf[i]); } } } catch (IOException e) { e.printStackTrace(); } finally { if (fileReader != null){ // 4.资源的关闭 try { fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String[] args) { File file = new File("hello.txt"); // 如果是 main方法 是 相较于 当前 工程下 System.out.println(file.getAbsolutePath()); // E:\java_workspace\JavaSE\hello.txt File file1 = new File("IO流\\hello.txt"); System.out.println(file1.getAbsolutePath()); // E:\java_workspace\JavaSE\IO流\hello.txt } /* 从内存中写出数据到硬盘的文件里。 说明:1.输出操作,对应的File可以不存在,如果不存在,在输出的过程中,会自动的创建此文件 file对应的硬盘中的文件如果存在:如果流使用的构造器是:FileWriter(file,false) / FileWriter(file) :对原有文件进行覆盖 如果流使用的构造器是:FileWriter(file,true): 不会对原有文件覆盖,而是在原有文件基础上追加内容 */ @Test public void testFileWriter() { FileWriter fileWriter = null; try { // 1.提供File类的对象,指明写出到的文件 File file = new File("hello1.txt"); // 2.提供FileWriter的对象,用于数据的写出 fileWriter = new FileWriter(file); // 3.写出的操作 fileWriter.write("I have a dream!"); } catch (IOException e) { e.printStackTrace(); } finally { if (fileWriter != null){ // 4.流资源的关闭 try { fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } } /* 把文件先读到内存 , 再从内存 写到硬盘中 相当于 文件的复制 注意:不能使用字符流来处理图片等字节数据 */ @Test public void testFileReaderFileWriter() { FileReader fileReader = null; FileWriter fileWriter = null; try { // 1.提供File类的对象,指明读入和写出的文件 File srcFile = new File("hello.txt"); File destFile = new File("hello2.txt"); // 2.创建输入流和输出流的对象 fileReader = new FileReader(srcFile); fileWriter = new FileWriter(destFile); // 3.数据的读入和写出 int len; char[] cbuf = new char[5]; while ((len = fileReader.read(cbuf)) != -1){ fileWriter.write(cbuf,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { if (fileWriter != null){ // 4.流的关闭 try { fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileReader != null){ try { fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }} /* 测试FileInputStream 和 FileOutputStream的使用 1. 对于文本文件(.txt,.java,.c,.cpp),使用字符流处理 2. 对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt...),使用字节流存储 */ @Test public void testFileInputStream() { FileInputStream fileInputStream = null; try { // 1.指明要读入的文件 如果读入的文件 有中文 可能会乱码 File file = new File("hello.txt"); // 2.指明要用的 字节流 fileInputStream = new FileInputStream(file); // 3.读数据 int len; byte[] buffer = new byte[5]; while ((len = fileInputStream.read(buffer)) != -1){ String str= new String(buffer, 0, len); System.out.println(str); } } catch (IOException e) { e.printStackTrace(); } finally { if (fileInputStream != null){ // 4.关闭资源 try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } /* 实现对图片的复制操作 */ @Test public void testFileInputOutputStream() { FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { // 1.指明 读入的文件 和 写出的文件 File srcFile = new File("1.jpg"); File destFile = new File("2.jpg"); // 2.指明 读图片的输入流 和 写出的输出流 fileInputStream = new FileInputStream(srcFile); fileOutputStream = new FileOutputStream(destFile); // 3.对数据的操作 复制的过程 byte[] buffer = new byte[5]; int len; while ((len = fileInputStream.read(buffer)) != -1){ fileOutputStream.write(buffer,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { if (fileOutputStream != null){ // 4.关闭流 try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileOutputStream != null){ try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
处理流之一:缓冲流
/** * @Author miaotiaojun * @Date 2020/8/31 17:34 */ /* 处理流之一:缓冲流的使用 1.缓冲流: BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter 2. 缓冲流:提高流的读取、写入的速度 3. 处理流,就是“套接”在已有流的基础上 */ /* 实现非文本文件的复制 */ @Test public void bufferedStreamTest() { BufferedInputStream bufferedInputStream = null; BufferedOutputStream bufferedOutputStream = null; try { // 1.造文件对象 File srcFile = new File("1.jpg"); File destFile = new File("3.jpg"); // 2.造流 // 2.1造节点流 FileInputStream fileInputStream = new FileInputStream(srcFile); FileOutputStream fileOutputStream = new FileOutputStream(destFile); // 2.2造缓冲流 bufferedInputStream = new BufferedInputStream(fileInputStream); bufferedOutputStream = new BufferedOutputStream(fileOutputStream); // 3.复制的细节:读取、写入 byte[] buffer = new byte[100]; int len; while ((len = bufferedInputStream.read()) != -1 ){ bufferedOutputStream.write(buffer,0,len); //bufferedOutputStream.flush();// 刷新缓冲区 } } catch (IOException e) { e.printStackTrace(); } finally { if (bufferedOutputStream != null){ try { bufferedOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (bufferedInputStream != null){ try { bufferedInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } // 4.资源关闭 // 要求:先关外层的流,再关闭内层的流 // 说明:关闭外层流的同时,内层流也会自动的进行关闭。关于内层流的关闭,我们可以省略 //fileOutputStream.close(); //fileInputStream.close(); } /* 使用BufferedReader 和 BufferedWriter实现文本文件的复制 */ @Test public void testBufferedReaderBufferedWriter(){ BufferedReader bufferedReader = null; BufferedWriter bufferedWriter = null; try { // 1.创建相应的流 bufferedReader = new BufferedReader(new FileReader(new File("hello.txt"))); bufferedWriter = new BufferedWriter(new FileWriter(new File("hello1.txt"))); // 2.读写操作 // 方式一、使用char[]数组 /*char[] buffer = new char[1024]; int len; while ((len = bufferedReader.read(buffer)) != -1){ bufferedWriter.write(buffer,0,len); }*/ // 方式二、使用String String buffer; while ((buffer = bufferedReader.readLine()) != null){ // 方法一 加 \n 换行 bufferedWriter.write(buffer + "\n"); // 读出的数据不包含换行符 // 方法二 bufferedWriter.newLine(); 表示换行 } } catch (IOException e) { e.printStackTrace(); } finally { if (bufferedWriter != null){ // 3.关闭流 try { bufferedWriter.close(); } catch (IOException e) { e.printStackTrace(); } } if (bufferedReader != null){ try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
其他流
/* 其他流的使用 1.标准的输入、输出流 System.in: 标准的输入流,默认从键盘输入 System.out: 标准的输出流,默认从控制台输出 2.打印流 PrintStream 和 PrintWrite 3.数据流 DataInputStream 和 DataOutputStream 用于读取或写出基本数据类型的变量或字符串 */ /* 从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续进行输入操作 直至当输入 e 或者 exit时,退出程序。 */ public static void main(String[] args) { BufferedReader bufferedReader = null; try { InputStreamReader inputStreamReader = new InputStreamReader(System.in); bufferedReader = new BufferedReader(inputStreamReader); System.out.println("请从键盘输入:"); while (true){ String data = bufferedReader.readLine(); if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)){ System.out.println("程序结束"); break; } String upperCase = data.toUpperCase(); System.out.println(upperCase); } } catch (IOException e) { e.printStackTrace(); } finally { if (bufferedReader != null){ try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
对象流
/* 对象流的使用: 1. ObjectInputStream 和 ObjectOutputStream 2. 作用:用于存储和读取基本数据类型数据 或对象的处理流 */ /* 序列化过程:将内存中的Java对象保存到磁盘中或者 通过网络传输出去 */ @Test public void testObjectOutputStream(){ ObjectOutputStream objectOutputStream = null; try { // 1.造流造对象 objectOutputStream = new ObjectOutputStream(new FileOutputStream("object.dat")); // 2.写出 objectOutputStream.writeObject(new String("苗条俊")); objectOutputStream.flush();// 刷新操作 } catch (IOException e) { e.printStackTrace(); } finally { if (objectOutputStream != null){ // 3.关闭资源 try { objectOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } /* 反序列化 过程:将磁盘文件中的对象还原为 内存中的一个对象 */ @Test public void testObjectInputStream(){ ObjectInputStream objectInputStream = null; try { objectInputStream = new ObjectInputStream(new FileInputStream("object.dat")); Object readObject = objectInputStream.readObject(); String str = (String)readObject; System.out.println(str); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectInputStream != null){ try { objectInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
文件随机访问
/* RandomAccessFile的使用 1. RandomAccessFile 直接继承于 java.lang.Object类,实现了 DataInput 和 DataOutput接口 2. RandomAccessFile 既可以作为输入流,又可以作为输出流 3. 如果 RandomAccessFile 作为输出流时,写出到的文件如果不存在,则在执行过程中自动创建, 如果写出到的文件存在,则会对原有文件内容进行覆盖。(默认从头覆盖,内存中有多少覆盖多少) 4. 通过 randomAccessFile.seek(角标) 方法的使用 实现 数据的插入 */ @Test public void testRandomAccessFile(){ RandomAccessFile randomAccessFile = null;// 只读 RandomAccessFile randomAccessFile1 = null;// 可读写 try { randomAccessFile = new RandomAccessFile(new File("1.jpg"),"r"); randomAccessFile1 = new RandomAccessFile(new File("3.jpg"),"rw"); byte[] buffer = new byte[1024]; int len; while ((len = randomAccessFile.read(buffer)) != -1){ randomAccessFile1.write(buffer,0, len); } } catch (IOException e) { e.printStackTrace(); } finally { if (randomAccessFile1 != null){ try { randomAccessFile1.close(); } catch (IOException e) { e.printStackTrace(); } } if (randomAccessFile != null){ try { randomAccessFile.close(); } catch (IOException e) { e.printStackTrace(); } } } } /* 使用RandomAccessFile实现数据的插入效果 */ @Test public void test3(){ RandomAccessFile randomAccessFile = null; try { randomAccessFile = new RandomAccessFile("hello.txt", "rw"); randomAccessFile.seek(2); // 将指针调到角标为 2的位置 // 保存 指针后面的所有数据到 StringBuilder 中 StringBuilder stringBuilder = new StringBuilder((int) new File("hello.txt").length()); byte[] buffer = new byte[20]; int len; while ((len = randomAccessFile.read(buffer)) != -1){ stringBuilder.append(new String(buffer,0,len)); } // 调回指针, 写入 “xyz” randomAccessFile.seek(2); randomAccessFile.write(stringBuilder.toString().getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { if (randomAccessFile != null){ try { randomAccessFile.close(); } catch (IOException e) { e.printStackTrace(); } } } }
注意点
相对路径在IDEA 和 Eclipse中使用的区别
IDEA : 如果使用单元测试方法,相对路径基于当前的module的 如果使用main()测试,相对路径是 基于当前 Project的。、
Eclipse : 单元测试方法还是 main(),相对路径都是基于当前project的。
序列化机制:允许把内存中的 Java 对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。 当其他 程序 获取了这种二进制流,就可以会服务成原来的 Java对象。
自定义类的 序列化:
实现 Serializable 接口 (此接口为标识接口 ,接口中什么都没有)
为该类提供一个全局常量:serialVersionUID
除了当前 类 需要实现 Serializable 接口之外,还必须保证其内部所有属性也必须是可序列化的。( 默认情况下,基本数据类型可序列化)
注意:不显示的 声明全局常量, 序列化之后, 若对 类进行修改 ,再 反序列化 会报异常
ObjectOutputStream 和 ObjectInputStream 不能序列化 static 和 transient 修饰的成 员变量 (但 是不报错 ,显示默认值)