【key】字节流体系
一、OutputStream类
OutputStream是一个抽象类,是表示字节输出流的所有类的超类。操作的数据都是字节,定义了输出字节流的基本共性功能方法。
常见方法:
二、FileOutputStream类
FileOutputStream类通过字节的方式写数据到文件,适合所有类型文件(图像、视频、文本文件等)
构造方法:
【示例】写入数据到文件
public class FileOutputStreamDemo { public static void main(String[] args) { // 创建存储数据的文件 File file = new File("test.txt"); OutputStream os = null; try { // 创建一个字节输出流,明确需要操作的文件 // 如果文件不存在,则创建;如果文件存在,则覆盖! os = new FileOutputStream(file); // 调用父类的方法存数据。立刻完成存储,不需要强制刷新存入 os.write("haha".getBytes()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 一定要判断 os 是否为 null,只有不为null时,才可以关闭资源 if(os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } } } |
给文件续写:
我们直接 new FileOutputStream(file)这样创建对象,写入数据,会覆盖原有的文件。那么我们想在原有的文件中续写内容怎么办呢?
在 FileOutputStream 的构造函数中,可以接受一个 boolean类型的值,如果值 true,就会在文件末位继续添加(默认是false)。
【示例】将数据写入文件
public class FileOutputStreamDemo { public static void main(String[] args) { // 创建存储数据的文件 File file = new File("test.txt"); OutputStream os = null; try { // 创建一个字节输出流,明确需要操作的文件 os = new FileOutputStream(file, true); // 调用父类的方法存数据。 os.write("haha".getBytes()); // 添加换行 os.write("\r\n".getBytes()); os.write("haha".getBytes()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 一定要判断 os 是否为null,只有不为null时,才可以关闭资源 if(os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } } } |
三、InputStream类
InputSteam是一个抽象类,是表示字节输入流的所有类的超类。操作的数据都是字节,定义了字节输入流的基本共性功能方法。
InputStream类中常见的方法:
四、FileInputStream
InputStream 有很多子类,其中子类 FileInputStream 可用来写入数据到文件。
FileInputStream通过字节的方式读取文件,适合读取所有类型文件(图像、视频、文本文件等)。
构造方法:
读取数据read()方法:
在读取文件中的数据时,调用 read 方法一次读一个字节,从而实现从文件中读取数据。
【示例】读取数据read()方法
public class FileInputStreamDemo { public static void main(String[] args) { FileInputStream fs = null; try { // 创建一个字节输入流对象,必须明确数据源 // 如果文件不存在,则抛出FileNotFoundException异常! fs = new FileInputStream("input.txt"); // 一次读一个字节 int len = 0; // 当返回值为-1,则数据读取完毕 while((len = fs.read()) != -1) { System.out.println((char)len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(fs != null) { try { fs.close(); } catch (IOException e) { e.printStackTrace(); } } } } } |
读取数据read(byte[])方法
在读取文件中的数据时,调用 read 方法,每次只能读取一个字节,太麻烦了,于是我们可以定义数组作为临时的存储容器,这时可以调用重载的 read 方法,一次可以读取多个字节。
【示例】读取数据read(byte[])方法
public class FileInputStreamDemo { public static void main(String[] args) { FileInputStream fs = null; try { // 创建一个字节输入流对象,必须明确数据源 fs = new FileInputStream("test.txt"); StringBuilder sb = new StringBuilder(); byte[] by = new byte[1024]; int len = 0;//保存获取到字节的长度 // 当返回值为-1,则数据读取完毕 while((len = fs.read(by)) != -1) { sb.append(new String(by, 0, len)); } // 输出内容 System.out.println(sb); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(fs != null) { try { fs.close(); } catch (IOException e) { e.printStackTrace(); } } } } } |
一次性读全部数据
使用FileInputStream类的available()方法,可以获取文件的所有字节个数。
但如果遇到文件数据量很大,容易造成内存溢出。
【示例】一次性读取文件全部数据
public class FileInputStreamDemo { public static void main(String[] args) { FileInputStream fs = null; try { // 创建一个字节输入流对象,必须明确数据源 fs = new FileInputStream("test.txt"); int len = 0; // 获取文件字节个数 int available = fs.available(); byte[] by = new byte[available]; // 一次性获取文件所有数据 fs.read(by); System.out.println(new String(by)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(fs != null) { try { fs.close(); } catch (IOException e) { e.printStackTrace(); } } } } } |
五、文件流的拷贝案例
【示例】字节流文件的拷贝案例
public class FileCopyDemo { public static void main(String[] args) throws IOException { // 明确字节流,输入流和源相关联,输出流和目的关联。 FileInputStream fis = new FileInputStream("E://movie.mp4"); // 输入流 FileOutputStream fos = new FileOutputStream("F://111.mp4"); // 输出流 // 定义一个数组,用于缓存取出来的数据 byte[] by = new byte[1024]; int len = 0; // 输入流,读取数据 while ((len = fis.read(by)) != -1) { // 输出流,保存数据 fos.write(by, 0, len); } // 关闭流 fis.close(); fos.close(); } } |