Java第十一天(实训学习整理资料(十)Java IO流)

目录

一、说明

二、Java中的IO操作分类

三、字符流

1、Writer

2、FileWriter

3、Reader

四、字节流

1、主要的类

2、FileOutputStream 文件字节输出流

3、FileInputStream 文件字节输入流

五、装饰器

1、BufferedInputStream

2、BufferedOutputStream

3、BufferedReader

4、BufferedWriter

六、系统流


一、说明

        I: Input 输入 ​

        O: Output 输出

输入和输出的方向:

        输入(Input): 从其他的资源读取数据到java程序。

        输出(Output):从java程序写出数据到其他资源载体。

二、Java中的IO操作分类

1、以字符作为最小单位进行操作,本质仍然是字节流(字符流)

2、以字节作为最小单位操作(字节流)

java中io操作的接口:

                                输入                 输出

字节流         - InputStream         - OutputStream

字符流         - Reader                 - Writer

三、字符流

1、Writer

        Writer是字符输出流:

                Writer是一个抽象类,不能直接使用,需要通过它的已知实现类操作

                操作的是文件 - Writer - OutputStreamWriter - FileWriter

2、FileWriter

构造方法:

        FileWriter(File file)

        FileWriter(File file, boolean append)

        FileWriter(String fileName)

        FileWriter(String fileName, boolean append)

说明:

        append 表示是否打开追加模式,

        true 表示将当前内容追加到原文件的末尾

        false 表示用当前内容重写原文件

常用方法:

        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);

说明:

        wite方法,不会立即将数据写入磁盘的文件中,而是先写入到缓冲区,目的是为了提高效率,当缓冲区满了后,数据会流入文件;如果不希望数据留在缓冲,则可以调用flush方法,立即刷新。

刷新缓冲区:

        void flush() //刷新缓冲区

关闭文件输出流:

        在关闭流时会自动调用flush方法

        void close()

3、Reader

Reader是字符输入流;

继承关系:Reader - InputStreamReader - FileReader

构造方法:

        FileReader(File file) //接受一个File对象

        FileReader(String fileName)//接受一个文件的路径字符串

常用方法:

        read() //读取第一个字符

        read(char[] cbuf, int offset, int length)

        read(char[] cbuf)

        read(CharBuffer target)

关闭文件输入流:

        void close();

案例:读取文件内容

//构建文件输入流 FileReader
        FileReader fileReader = new FileReader("D:" + PS + "test" + PS + "out.txt");
        char[] chars = new char[4];
        int readChars = 0;
        while ((readChars = fileReader.read(chars))!=-1){
            String str = new String(chars, 0, readChars);
            System.out.println(str);
        }
        fileReader.close();

练习:

1、利用File实现递归删除,写死。

2、统计工作空间下Java文件,并输出代码行数

3、复制文件

四、字节流

1、主要的类

        InputStream

        OutputStream

字符流的底层也是基于字节流实现,字节流比较通用。

2、FileOutputStream 文件字节输出流

构造方法:

        FileOutputStream(String name, boolean append)

        FileOutputStream(File file, boolean append)

常用方法:

        write(byte b[])

案例代码:

//定义文件路径
		  String path = "E:\\java72_projects\\java-api\\src\\cn\\hxzy\\apimod\\day08\\files\\erro.log";

		  FileOutputStream fileOutputStream = null;

		  try {
			  //打开文件
			  fileOutputStream = new FileOutputStream(path);

			  //操作文件
			  String content = "hello java,欢迎使用idea!";
			  fileOutputStream.write(content.getBytes("UTF-8"));
			  //fileOutputStream.flush();

		  } catch (Exception e) {
			  e.printStackTrace();
		  } finally {
			  //关闭文件
			  if(fileOutputStream != null) {
				  try {
					  fileOutputStream.close();
				  } catch (IOException e) {
					  e.printStackTrace();
				  } finally {
					  fileOutputStream = null;
				  }
			  }
		  }
3、FileInputStream 文件字节输入流

构造方法:

        FileInputStream(File file)

        FileInputStream(String name)

常用方法:

        int read()

        int read(byte[] b)

案例代码:

//定义文件路径
		  String path = "E:\\java72_projects\\java-api\\src\\cn\\hxzy\\apimod\\day08\\files\\erro.log";
		  String path1 = "E:\\java72_projects\\java-api\\src\\cn\\hxzy\\apimod\\day08\\files\\erro-out.log";


		  //构建字节输入流
		  FileInputStream fileInputStream = null;

		  //构建字节输出流
		  FileOutputStream fileOutputStream = null;
		  try {
			  fileInputStream = new FileInputStream(path);

			  fileOutputStream = new FileOutputStream(path1);

			  //操作
			  byte[] btns = new byte[2];
			  //读取2个字节-》btns
			  int count = 0;
			  //这种问题如何解决?
			  while ((count = fileInputStream.read(btns)) != -1) {
				  //将读取的文件数据写出到其他文件
				  //fileOutputStream.write(btns, count);
				  fileOutputStream.write(btns, 0, count);

				  //控制台打印
				  /*String result = new String(btns, "UTF-8");
				  System.out.println(result);*/
			  }

		  } catch (Exception e) {
			  e.printStackTrace();
		  } finally {
			  //先关输入还是先关输出?  --- 先关输入
			  //关闭
			  if(fileInputStream != null) {
				  try {
					  fileInputStream.close();
				  } catch (IOException e) {
					  e.printStackTrace();
				  } finally {
					  fileInputStream = null;
				  }
			  }

			  if(fileOutputStream != null) {
				  try {
					  fileOutputStream.close();
				  } catch (IOException e) {
					  e.printStackTrace();
				  } finally {
					  fileOutputStream = null;
				  }
			  }
		  }

练习:通过字节输入和输出流实现将视频文件进行复制。

五、装饰器

        由于InputStream&OutputStream没有缓冲区,导致java程序需要直接与磁盘进行IO操作,

        增加了BufferedInputStream&BufferedOutputStream

1、BufferedInputStream

构造方法:

        BufferedInputStream(InputStream in)

        BufferedInputStream(InputStream in, int size)

主要方法:

        synchronized void flush()

2、BufferedOutputStream

构造方法:

        BufferedOutputStream(OutputStream out)

        BufferedOutputStream(OutputStream out, int size)

3、BufferedReader

        String readLine() //读取一行

4、BufferedWriter

        在字符的装饰类上扩展了标记功能

        //用于标记当前文件中的操作位置

        void mark(int readAheadLimit)

        //将文件操作指针重置到mark标记位

        void reset()

案例代码:

String path = "E:\\java72_projects\\java-api\\src\\cn\\day08\\files\\main.txt";

//从文件中读取字符  file read
FileReader fileReader = new FileReader(path);
BufferedReader bufferedReader = new BufferedReader(fileReader);

//char cha = (char)bufferedReader.read();

System.out.println((char)bufferedReader.read());
bufferedReader.mark(10);
System.out.println((char)bufferedReader.read());
System.out.println((char)bufferedReader.read());
System.out.println((char)bufferedReader.read());
bufferedReader.reset();
System.out.println((char)bufferedReader.read());
System.out.println((char)bufferedReader.read());
System.out.println((char)bufferedReader.read());

        //执行结果: a,b,c,d,b,c,d

六、系统流

        System.err 标准错误输出流

        System.out 标准输出流

        System.in 标准输入流

        Scanner 扫描器

案例:

Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        System.out.println(str);
		
		
	利用装饰器配合系统标准输入流实现扫描器功能:
		BufferedInputStream bufferedInputStream = new BufferedInputStream(System.in);

        System.out.println("请输入内容:");
        byte[] bt = new byte[255];
        int read = bufferedInputStream.read(bt, 0, bt.length);
        String str = new String(bt, 0, read, "utf-8");

        System.out.println("您接收的内容是:"+str);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值