目录
又称输入输出流
注:流开启之后一定要关闭,在finally里关闭
关闭规则:先用后关,后用先关
输入流:从硬盘上读取数据到内存。 (读)
输出流:从内存写出数据到硬盘。 (写)
一个文件在传输过程中经历了多次拷贝,I0性能本身就很低。
File类不能操作文件的内容
字节流:是一个字节一个字节的操作。二进制操作。操作任意类型的文件。.
字符流:是一个字符一个字符的操作。一个字符两个字节,主要用来处理文本文件
.txt,.java,.py,.xml,.properties,.html,.css,.js...
节点流:直接操作一个特定的I0设备。
处理流:在节点流的基础上,做进一步的处理。
java中输入/输出流常用的流:
字节输入流/输出流 字符输入/输出流
抽象基类 :InputStream OutPutStream,一般带Input/Output的都是字节流
Reader,Writer是字符流
访问文件: FileInputStream FileOutputStream FileReader FileWriter
(↑是节点流)
缓冲流 BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter
(↑是处理流)
操作对象 ObjectInputStream ObjectOutputStream
那么流到底怎么用?
输入流:就是一点一点的往内存里读数据!!!
1.创建一 个FileInputStream对象
2.定义一个标记,用来控制输入流的读取
3.循环读取,如果读取到了-1,说明读取到了文件的末尾,循环结束
4.关闭资源。
注意: 我们发现一个流读完了,就没有了,不能再读了
当一个流读完之后会默认调用mark和reset方法来进行记录和重置,这个流就已经重置到了上次读完的位置
所以就无法再次读取内容,并不是读完一次就关闭了流
- @Test
- public void test0() throws IOException {
- InputStream inputStream = new FileInputStream("C:\\summerjava\\abc\\a.txt");
- //开始读的操作,read方法,返回值是int,当返回值为一1时,说明文件读取到了尾部
- // 读取文件是否结束的标记
- int read;
- // 字节流读数据的时候一个字节一个字节去读
- //循环读取
- while ((read = inputStream.read()) != -1) {
- System.out.print(read + " ");
- }
- inputStream.close();//关闭资源如果有关闭的操作,就不要抛异常,建议都try/catch
FileOutputStream构造器:
boolean append参数:如果传入true,则代表在原有基础上追加,不覆盖
如果传入false,或者不传,覆盖原有内容
如果没有文件,他会自己帮你创建,并写入
- @Test
- public void test01(){
- OutputStream outputStream=null;
- try {
- outputStream = new FileOutputStream("a.txt",true);
- outputStream.write("\r\n".getBytes());
- outputStream.write("八月正午的阳光都没你耀眼".getBytes());
- outputStream.write(97);
- } catch (Exception e) {
- throw new RuntimeException(e);
- } finally {
- try {
- outputStream.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
两个流:
FileInputStream:把对应文件的内容读取出来
FileOutputStream:把读到的内容写出去
read(byte[])
write(byte[])
- @Test
- public void test01(){
- InputStream inputStream = null;
- OutputStream outputStream = null;
- try {
- inputStream = new FileInputStream("E:\\workspace\\vscode\\HTML\\20220709\\img\\lijian.jpeg");
- outputStream = new FileOutputStream("e:/lijian.jpeg");
-
- byte [] buf = new byte[3];
- int len;
-
- // 循环读进来
- while((len = inputStream.read(buf)) != -1){
- // 写出去
- outputStream.write(buf,0,len);
- }
- System.out.println("文件复制完毕...");
- } catch (FileNotFoundException e) {
- throw new RuntimeException(e);
- } catch (IOException e) {
- throw new RuntimeException(e);
- } finally {
- // IOUtil.closeIO(inputStream,outputStream);
- try {
- if(Objects.nonNull(outputStream)){
- outputStream.close();
- int i = 10 / 0;
- }
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- try {
- if(Objects.nonNull(inputStream)){
- inputStream.close();
- }
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
-
- }
- }
字符输入流:Writer wirter = new FileWriter("c:/a.txt");
字符输出流:Reader reader = new FileReader("c:/a.txt");
- //输入流
- @Test
- public void test02() {
- Writer writer = null;
- try {
- writer = new FileWriter("e:/bbb.txt");
- writer.write("阿里巴巴规约手册...");
- System.out.println("数据写出成功...");
- } catch (IOException e) {
- throw new RuntimeException(e);
- }finally {
- IOUtil.closeIO(null,writer);
- }
- }
- //输出流
- @Test
- public void test01() {
- Reader reader = null;
- try {
- reader = new FileReader("e:/aaa.txt");
- int len;
- char [] buf = new char[10];
- while((len = reader.read(buf)) != -1){
- System.out.println(new String(buf,0,len));
- }
- } catch (FileNotFoundException e) {
- throw new RuntimeException(e);
- } catch (IOException e) {
- throw new RuntimeException(e);
- } finally {
- IOUtil.closeIO(reader,null);
- }
- }
缓冲流,只能处理纯文本文件:
- @Test
- public void test02() {
- BufferedWriter bufferedWriter = null;
- try {
- bufferedWriter = new BufferedWriter(new FileWriter("c:/a.txt"));
- bufferedWriter.write("今天是星期二,天气凉爽!");
- System.out.println("数据写出成功!");
- } catch (IOException e) {
- throw new RuntimeException(e);
- } finally {
- IOUtil.closeIO(null,bufferedWriter);
- }
- }
-
- @Test
- public void test01() {
- BufferedReader bufferedReader = null;
- try {
- bufferedReader = new BufferedReader(new FileReader("c:/aaa.txt"));
- String str;
- while((str = bufferedReader.readLine()) != null) {
- System.out.println(str);
- }
- } catch (FileNotFoundException e) {
- throw new RuntimeException(e);
- } catch (IOException e) {
- throw new RuntimeException(e);
- } finally {
- IOUtil.closeIO(bufferedReader,null);
- }
- }
传入输入输出流,进行判断是否为空并关闭。
-
- import java.io.*;
- import java.util.Objects;
-
- public class IOUtil {
-
- public static void closeIO(Reader reader, Writer writer){
- if(Objects.nonNull(reader)) {
- try {
- reader.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- if(Objects.nonNull(writer)){
- try {
- writer.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
-
- public static void closeIO(InputStream inputStream, OutputStream outputStream) {
- if(Objects.nonNull(inputStream)) {
- try {
- inputStream.close();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- if(Objects.nonNull(outputStream)){
- try {
- outputStream.close();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- }
- }
Reader reader = new FileReader("e:/aaa.txt")
bufferedReader = new BufferedReader(reader);
- BufferedReader bufferedReader = null;
- BufferedWriter bufferedWriter = null;
- try {
- bufferedReader = new BufferedReader(new FileReader("e:/aaa.txt"));
- bufferedWriter = new BufferedWriter(new FileWriter("e:/bbb.txt"));
-
- String str;
- while((str = bufferedReader.readLine()) != null){
- bufferedWriter.write(str);//写入
- bufferedWriter.newLine();//换行
- }
- System.out.println("文件复制成功!!!");
-
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- IOUtil.closeIO(bufferedReader,bufferedWriter);
- }