IO流

mac2022-06-30  105

概述:

IO流介绍OutputStream: 输出字节流写出字节数组数据追加续写字节流一次读取一个字节String和byte[]之间转换字节流一次读取一个字节数组的数据字节流复制文件字符流读取数据 10.字符流写数据

1.IO流介绍

作用:

可以往文件中写数据,可以读取文件中的数据

根据数据的流向分为:输入流和输出流。

输入流 :把数据从 其他设备 上读取到 内存 中的流。输出流 :把数据从 内存 中写出到 其他设备 上的流。

格局数据的类型分为:字节流和字符流。

字节流 :以字节为单位,读写数据的流。(通用的,可以操作任何类型的数据)字符流 :以字符为单位,读写数据的流。(方便用户操作文本的)

注意:

计算中所有的数据视频,音频,图片,文字…都是以字节的形式存储的

IO流的四个爹: 总结

特点:

这四个父类都是抽象的,我们要使用子类

总结:

看到Stream的就是字节流看到er的就是字符流

IO流的使用步骤:

1.创建一个IO流 2.写数据 3.关流、关流、关流(重要的事情说三遍)

2.OutputStream: 输出字节流

方法:

void write​(int b) 将一个字节写入流中

子类:

FileOutputStream​(String name) 通过路径名字符串创建一个FileOutputStream​FileOutputStream​(File file) 通过File对象创建一个FileOutputStream​

注意:

构造方法会创建文件,如果文件已经存在会先删除这个文件再创建

public class FOSWrite { public static void main(String[] args) throws IOException { // 使用文件名称创建流对象 FileOutputStream fos = new FileOutputStream("fos.txt"); // 写出数据 fos.write(97); // 写出第1个字节 fos.write(98); // 写出第2个字节 fos.write(99); // 写出第3个字节 // 关闭资源 fos.close(); } } // 输出结果:abc

3.写出字节数组:

OutputStream:

void write​(byte[] b) 将数组中的数据写入流中void write​(byte[] b, int off, int len) 将数组的一部分数据写入流中int off: 从哪个索引开始int len:写多少个 public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("day09demo\\abc\\2.txt"); byte[] buf = new byte[] {65, 66, 67, 68, 69}; // void write​(byte[] b) 将数组中的数据写入流中 fos.write(buf); // void write​(byte[] b, int off, int len) 将数组的一部分数据写入流中 // int off: 从哪个索引开始 // int len:写多少个 fos.write(buf, 2, 3); // 字符串转成数组 byte[] bytes = "我好帅啊".getBytes(); fos.write(bytes); fos.close(); }

4.数据追加续写

追加数据:

FileOutputStream​(String name, boolean append)FileOutputStream​(File file, boolean append)当append设置为true时,不会删除之前的文件,追加写数据

换行:

在Windows中 “\r\n” 表示换行

public static void main(String[] args) throws IOException { // FileOutputStream​(String name, boolean append) append设置为true时,不会删除之前的文件,追加写数据 FileOutputStream fos = new FileOutputStream("day09demo\\abc\\2.txt", true); // 换行 fos.write("\r\n".getBytes()); fos.write(97); fos.close(); }

5.字节流一次读取一个字节

InputStream: 字节输入流

int read​() 从输入流读取下一个字节数据, 返回读取到的字节

子类

FileInputStream​: 文件字节输入流,从文件中来读取数据

构造方法:

FileInputStream​(String name) 通过路径名字符串创建FileInputStream​FileInputStream​(File file) 通过File对象创建FileInputStream​ public static void main(String[] args) throws IOException { // FileInputStream​(String name) 通过路径名字符串创建FileInputStream​ /*FileInputStream fis = new FileInputStream("day09demo\\abc\\2.txt"); // int read​() 从输入流读取下一个字节数据, 返回读取到的字节 int b = fis.read(); System.out.println("b = " + (char)b); // H b = fis.read(); System.out.println("b = " + (char)b); // e b = fis.read(); System.out.println("b = " + (char)b); // l b = fis.read(); System.out.println("b = " + (char)b); // l b = fis.read(); System.out.println("b = " + (char)b); // o b = fis.read(); System.out.println("b = " + b); */// -1 读取不到数据返回-1 // 以上代码都是重复代码,我们使用循环来解决,不知道循环次数,使用while循环 FileInputStream fis = new FileInputStream("day09demo\\abc\\2.txt"); int b; // 保存读取到的数据 // fis.read(): 读取流中的数据 // b = fis.read(): 将读取到的数据保存到b中 // b != -1: 让b和-1对比 while ((b = fis.read()) != -1) { System.out.println("b = " + (char)b); } fis.close(); }

6.String和byte[]之间转换

1.String转byte[]使用哪个方法?

“你好”.getBytes();

2.byte[]转String使用哪个方法?

String(byte[] bytes)String(byte[] bytes, int offset, int length) public static void main(String[] args) { // String转byte[] byte[] bytes = "你好".getBytes(); // byte[]转String byte[] buf = new byte[] {65, 66, 67, 68, 69}; // String(byte[] bytes) String str = new String(buf); System.out.println("str = " + str); // str = ABCDE // String(byte[] bytes, int offset, int length) 将字节数组的一部分转成字符串 // int offset: 从数组哪个索引开始 // int length: 长度 String str2 = new String(buf, 1, 3); System.out.println("str2 = " + str2); // BCD }

7.字节流一次读取一个字节数组的数据

1.FileInputStream一次读取一个字节数组的数据使用哪个方法?

int read​(byte[] b) 读取多个字节,保存到数组中,返回读取到的个数

2.如何循环读取一个字节的数组?

byte[] buf = new byte[1024]; // 保存读取到的多个内容,容器 int len; // 保存读取到的个数 while ((len = fis.read(buf)) != -1) { 操作; }

3.一次读取一个字节的数组好处?

效率高

public static void main(String[] args) throws IOException{ FileInputStream fis = new FileInputStream("day09demo\\abc\\2.txt"); // 一个开发中长度写1024的整数倍,通常写1024 * 4 byte[] buf = new byte[3]; // 保存读取到的数据,相当于一个容器 int len; // 保存读取到的个数 // fis.read(buf): 将读取的多个数据放到数组中 // len = fis.read(buf): 将读取到的个数保存到len中 // len != -1: 判断len不等于-1 while ((len = fis.read(buf)) != -1) { System.out.println(new String(buf, 0, len)); } // 关闭流 fis.close(); }

8.字节流复制文件

复制文件的步骤:

1.创建输入流,用于读取数据 2.创建输出流,用于写数据 3.循环读写 4.关闭流

public static void main(String[] args) throws Exception { long start = System.currentTimeMillis(); // copy01(); // 597 copy02(); // 2 long end = System.currentTimeMillis(); System.out.println("消耗时间: " + (end - start)); } public static void copy02() throws Exception { // 1.创建输入流,用于读取数据 FileInputStream fis = new FileInputStream("day09demo/abc/xyz.png"); // 2.创建输出流,用于写数据 FileOutputStream fos = new FileOutputStream("c:/MyFileTest/xyz2.png"); // 3.循环读写,一个字节数组 byte[] buf = new byte[1024 * 4]; // 保存读取的内容,容器 int len; // 保存读取的个数 while ((len = fis.read(buf)) != -1) { fos.write(buf, 0, len); // 读取到几个就写几个 } // 4.关闭流 fos.close(); fis.close(); } public static void copy01() throws Exception { // 1.创建输入流,用于读取数据 FileInputStream fis = new FileInputStream("day09demo/abc/xyz.png"); // 2.创建输出流,用于写数据 FileOutputStream fos = new FileOutputStream("c:/MyFileTest/xyz.png"); // 3.循环读写,一个字节 int b; // 保存读取到的数据 while ((b = fis.read()) != -1) { fos.write(b); } // 4.关闭流 fos.close(); fis.close(); }

9.字符流读取数据

为什么要用字符流:

字节流理论上可以操作任何数据.但是字节流操作文字不方便 --------- idea使用utf-8编码,一个中文占用3个字节

Reader: 字符输入流

int read​() 读一个字符,返回值就是读取到的字符int read​(char[] cbuf)读取多个字符保存到字符数组中,返回读取的个数

子类FileReader: 文件字符输入流, 可以读取文件的字符数据

FileReader​(String fileName) 通过路径名字符串创建FileReaderFileReader​(File file) 通过File对象创建FileReader public static void main(String[] args) throws Exception { FileReader fr = new FileReader("day09demo\\abc\\4.txt"); char[] chs = new char[2]; // 保存读取的内容 int len; // 保存读取的个数 while ((len = fr.read(chs)) != -1) { System.out.println(new String(chs, 0, len)); } fr.close(); }

10.字符流写数据

Writer: 字符输出流

void write​(int c) 写一个字符void write​(char[] cbuf) 写入一个字符数组void write​(char[] cbuf, int off, int len) 写入字符数组的一部分void write​(String str) 写一个字符串void write​(String str, int off, int len) 写一个字符串的一部分

子类FileWriter​: 文件字符输入流,写字符到文件

FileWriter​(String fileName) 通过路径名字符串创建FileWriterFileWriter​(File file) 通过File对象创建FileWriter

注意:

flush: 刷新缓冲区, flush后还能再写数据close: 关闭流,会先刷新缓冲区, close后不能再写数据 public static void main(String[] args) throws IOException { FileWriter fw = new FileWriter("day09demo\\abc\\5.txt"); // void write​(int c) 写一个字符 fw.write('我'); char[] chs = new char[] {'我', '想', '要', '个', '女', '同', '桌'}; // void write​(char[] cbuf) 写入一个字符数组 fw.write(chs); // void write​(char[] cbuf, int off, int len) 写入字符数组的一部分 fw.write(chs, 4, 2); String str = "寒冷的天气可以使人年轻,张大爷今年70岁了,冻的像个孙子似的"; // void write​(String str) 写一个字符串 fw.write("\r\n"); fw.write(str); // void write​(String str, int off, int len) 写一个字符串的一部分 fw.write("\r\n"); fw.write(str, 0, 3); /* flush: 刷新缓冲区, flush后还能够再写数据,相当于保存 */ // fw.flush(); // fw.write("嘻嘻"); // fw.flush(); /* close: 关闭流, 在关闭前会刷新缓冲区 close后不能再写数据,相当于关闭 */ fw.close(); // fw.write("嘻嘻"); }
最新回复(0)