16.IO流

字符流

package com.atguigu.java1;
/*
一、流的分类
按照操作数据单位:  字节流(8bit)、 字符流(16bit)
按照流向: 输入流、输出流
流的角色: 节点流、 处理流

二、流的体系结构
抽象基类            节点流(或文件流)        缓冲流(处理流的一种)
InputStream        FileInputStream           BufferedInputStream
OutputStream       FileOutputStream          BufferedOutputStream
Reader             FileReader                BufferedReader
Writer             FileWriter                bufferedWriter
 */

import org.junit.Test;

import java.io.*;

public class FileReadWriteTest {
    public static void main(String[] args) {
        File file = new File("Hello.txt");//相对于工程下
        System.out.println(file.getAbsolutePath());//D:\IntelliJ  IDEA 2020\workspace\JavaSenior\Hello.txt
    }
    /*
     将Day06下的hello.txt文件内容读入到程序中,并输出到控制台
     1.read():返回读入的一个字符,如果达到文件末尾,返回-1
     2.异常的处理,为了保证流资源一定可以执行关闭操作,一定要使用try-catch-finally处理
     3.读入的文件,一定要存在,否则会报FileNotFountException
     */
    @Test
    public void test1()  {
        FileReader fr = null;
        try {
            //1.实例化File类的对象,指明要操作的文件
            File file = new File("Hello.txt");//相较于当前module
            //2.提供具体的流
            fr = new FileReader(file); //如果实例化出异常,fr未实例化,则fr为空,在fr.close操作会出现空指针异常
            //3.数据的读入过程
            //方式一:
//        int data = fr.read();
//        while(data != -1){
//            System.out.print((char) data);//h e l l o W o r d
//            data = fr.read();
//        }
            int data1;
            while((data1 = fr.read()) != -1){
                System.out.println((char)data1);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.流的关闭操作
            if (fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /*
       read():重载方法
       read(char[] cbuf):返回每次读入cbuf数组中的字符个数.如果达到文件末尾返回-1
     */
    @Test
    public void test2()  {
        FileReader fr = null;
        try {
            File file = new File("Hello.txt");

            fr = new FileReader(file);
            char[] cbuf = new char[5];
            int len;
            while ((len = fr.read(cbuf))!=-1){
                //错误写法  第二次读取world存放在cbf中,在第三次读取123时,时123覆盖数组中的wor
//                for (int i = 0;i<cbuf.length;i++){
//                    System.out.print(cbuf[i]);//helloWorld123ld
//                }
                //正确的写法
//                for (int i =0 ; i< len;i++){
//                    System.out.println();
//                }
                //错误写法
//                String str = new String(cbuf);
//                System.out.println(str);//hello world 123ld
                String str = new String(cbuf,0,len);
                System.out.println(str);//hello world 123
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    /*
    从内存中写出数据到硬盘的文件里
    说明:1.输出操作 对应的file文件可以不存在,并不会报异常
          2.File对应的硬盘中的文件如果不存在,可以在输出时,自动创建此文件,
            File对应的硬盘中的文件如果存在,①file使用的构造器是FileWriter(file,false)或者FileWriter(file),对原有文件的覆盖
                                         ②file使用的构造器是FileWriter(file,true):则不会对原有文件覆盖,而是在原有文件基础上追加新内容
    */
    @Test
    public void test3()  {
        FileWriter fw = null;
        try {
            //1.提供File类的对象,指明写到的文件
            File file = new File("hello1.txt");
            //2.提供FileWriter的对象,用于数据的写出
            fw = new FileWriter(file,false);
            //3.写出的具体的操作
            fw.write("I have a dream\n");
            fw.write("you need to have a dream");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fw != null){
                    fw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //4.流关闭的操作
    }
    /*
    文件的复制:将读入的内容再写出去
     */
    @Test
    public void test4()  {
        FileReader fr = null;
        FileWriter fw = null;
        try {
            //1.创建file的对象,指明读入和写出的文件
            File srcFile = new File("hello.txt");
            File destFile = new File("hello2.txt");
            //2.创建输入流和输出流的对象
            fr = new FileReader(srcFile);
            fw = new FileWriter(destFile);
            //3.数据的读入和写出操作
            char[] cbuf = new char[5];
            int len;//记录每次读入cbuf数组中的字符个数
            while((len = fr.read(cbuf))!=-1){
                //每次写出len个字符
                fw.write(cbuf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭流操作
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

 

上一篇:学习java21天


下一篇:用Python搜索指定文件夹下所有文件,并生成链接网页