文件流 之 字符流 FileReader、FileWriter

时间:2024-4-21    作者:老大夫    分类: JAVA


使用File创建对象后,就使用IO流对文件进行操作

所有流的使用方法都基本一致,固定的规范流程

注意:使用try catch处理异常 最后finally 结束close

对于输入流来讲,要求File类的对象对应的物理磁盘上的文件必须存在。否则,会报FileNotFoundException对于输出流来讲,
File类的对象对应的物理磁盘上的文件可以不存在。
如果此文件不存在,则在输出的过程中,会自动创建此文件,并写出数据到此文件中。>如果此文件存在,
使用 FileWriter(File file)或 FileWriter(File file,false):输出数据过程中,会新建同名的文件对现有的文件进行覆盖,
FileWriter(File file,true):输出数据过程中,会在现有的文件的末尾追加写出

1. 使用方法

  1. 创建File对象
 File file = new File("D:\\io\\hello.txt");
  1. 创建IO流
FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter(newFile,false);
  1. 读取、输出数据
读入:read(char[] cbuffer)
写出:write(String str)/write(char[]cbuffer,0,len)
  1. 关闭资源
fr.close
fw.close

2. 举例

public class ReaderTest {
    @Test
    public void test01() throws IOException {
        //1. 创建File对象
        File file = new File("D:\\io\\hello.txt");
        //2. 创建字符流
        FileReader fr = new FileReader(file);
        //3. 读取数据

//        方式一
//        int data;
//        data=fr.read();
//
//        while(data!=-1){
//            System.out.println((char)data);
//            data=fr.read();
//        }

//        方式二:
        int data;
        while((data=fr.read())!=-1){
            System.out.println((char)data);
        }
        //4. 资源流的关闭操作
        fr.close();
    }

    /*
   throw抛出异常的方式并不合适,不能保证异常时资源一定关闭
   使用try catch的方法处理异常
   快捷键 ctrl alt + t
     */
    @Test
    public void test02() {
        FileReader fr = null;
        try {
            //1. 创建File对象
            File file = new File("D:\\io\\hello.txt");
            //2. 创建字符流
            fr = new FileReader(file);
            //3. 读取数据

            int data;
            while((data=fr.read())!=-1){
                System.out.println((char)data);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //4. 资源流的关闭操作
            try {
               if(fr!=null){
                   fr.close();
               }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /*
    正确完整的写法
     */
    @Test
    public void test03() {
        FileReader fr = null;

        try {
            //1. 创建File对象
            File file = new File("D:\\io\\hello.txt");
            //2. 创建字符流
            fr = new FileReader(file);
            //3. 读取数据
            char[] cBuffer = new char[5];

            int len;
            while((len= fr.read(cBuffer))!=-1){
                for(int i=0;i<len;i++){
                    System.out.println(cBuffer[i]);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //4. 资源流的关闭操作
                try {
                    if(fr!=null)
                        fr.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
        }
    }

    /*
    需求:将内存内容输出到文件
     */

    @Test
    public void test04() {
        FileWriter fw = null;
        try {
            //1. 创建File对象
            File file = new File("D:\\io\\hello.txt");
            //2. 创建输出字符流,默认false不进行追加,是覆盖原内容
//            fw = new FileWriter(file,false);
            // 创建输出字符流,ture为追加内容,原内容不变
            fw = new FileWriter(file,true);

            //3. 输出数据,输出方法write
            char[] cBuffer = new char[]{'b','c','d','1','2','3'};

            fw.write(cBuffer);

            System.out.println("输出成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //4. 资源流的关闭操作
            try {
                if(fw!=null)
                    fw.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /*
    需求:复制hello.txt命名为hello_copy.txt
     */
    @Test
    public void test05() {
        FileReader fr=null;
        FileWriter fw=null;
        try {
            //1. 创建File对象
            File file = new File("D:\\io\\hello.txt");
            File newFile = new File("D:\\io\\hello_copy.txt");
            //2. 创建输出字符流,默认false不进行追加,是覆盖原内容
            fr = new FileReader(file);
            fw = new FileWriter(newFile,false);

            //3. 输出数据,输出方法write
            char cBuffer[] = new char[5];
            int len;
            while((len=fr.read(cBuffer))!=-1){
                fw.write(cBuffer,0,len);
            }
            System.out.println("输出成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //4. 资源流的关闭操作
            try {
                if (fr!=null){
                    fr.close();
                }
                if(fw!=null){
                    fw.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

}


扫描二维码,在手机上阅读

推荐阅读: