Java中那些我不明白的小知识(七) -- IO流(下)

文件操作字符流

  • 字符流特征

字符流 = 字节流 + 解码过程

  • 文件操作输入字符流

构造方法:

FileReader(File file)

根据File类对象创建对应的FileReader字符流输入对象

FileReader(String pathName)

根据String类型文件路径创建对应的FileReader字符流输入对象

如果文件不存在,抛出异常  FileNotFoundException

Method成员方法:

int read();

读取文件中的一个字符数据,通过返回值返回,返回值类型是int类型,但是在int类型中有且只有低16位数据有效

int read(char[] arr);

读取文件中的数保存到字符数组中,返回值类型是读取到的字符个数

int read(char[] arr , int off , int len);

读取文件中的数据保存到字符数组中,要求从数组中下标为offset开始,到len结束,返回值类型是读取到的字符个数

以上方法,如果读取到文件默认,返回值为-1 EOF End Of File

如果读取操作工作中,出现问题,抛出异常 IOException

package com.wcc.a;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * 文件字符输入流,只能读取普通文本
 * 读取文本内容时方便快捷
 * @Author kk
 * @Date 2020/3/28 20:06
 */
public class FileReaderTest {
    public static void main(String[] args) {
        FileReader reader = null;
        try {
            reader = new FileReader("F:\\Test02\\copytemp.txt");

            //一次读取一个字符
            char[] chars = new  char[1];
            int readCount = 0;
            while ((readCount = reader.read(chars)) != -1){
                System.out.print(new String(chars,0,readCount));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
  • 文件操作字符输出流

构造方法:

FileWriter(File file);

根据File类对象创建对应文件的文件操作输出字符流

FileWriter(String pathName);

根据String类型文件路径创建对应文件的文件操作输出字符流

FileWriter(File file , boolean append);

根据File类对象创建对应文件的文件操作输出字符流,ture是要求为追加写

FileWriter(String pathName,boolean append);

根据String类型文件路径创建对应文件的文件操作输出字符流,并要求为追加写

如果创建FileWriter对象时,这里文件不存在,路径合法,这里会创建对应的操作文件。如果路径不合法,抛出异常FileNotFoundException

Method成员方法:

void write(int ch);

写入一个char类型数据到文件中

void write(char[] arr);

写入一个char类型数组到文件中

void write (char[] arr , int  offset ,int length);

写入一个char类型数组到文件中,要求从offset下标位置开始读取数组数据,长度为length

如果写入数据操作过程中,发生问题,这里会有一个IOException

package com.wcc.a;

import java.io.FileWriter;
import java.io.IOException;

/**
 * 文件字符输出流,负责写
 * @Author kk
 * @Date 2020/3/28 20:15
 */
public class FileWriterTest {
    public static void main(String[] args) {
        FileWriter writer = null;
        try {
            writer = new FileWriter("F:\\Test02\\copytemp.txt");
            char[] chars = {'中','国','最','牛','逼'};
            writer.write(chars);
            //也可以直接写:
            //writer.write("我是中国人我很骄傲");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(writer != null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

字符流完成文件的拷贝,只能拷贝普通的文本文件

package com.wcc.a;

import java.io.*;

/**
 * 使用字符流拷贝文件,只能拷贝普通的文本文件
 * @Author kk
 * @Date 2020/3/28 20:22
 */
public class FileCopy {
    public static void main(String[] args) {
        FileReader reader = null;
        FileWriter writer = null;

        try {
            //读
            reader = new FileReader("F:\\Test02\\copytemp.txt");
            //写
            writer = new FileWriter("F:\\Test\\copytemp2.txt");

            //一次拷贝1kb
            char[] chars = new char[1024 * 1];
            int readCount = 0;
            while((readCount = reader.read(chars)) != -1){
                writer.write(chars);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

缓冲流(使用时不许要指定byte或者char数组,自带缓冲)

构造方法:
    BufferedReader(Reader in) 创建一个使用默认大小输入缓冲区的缓冲字符输入流
    BufferedReader(Reader in, int sz) 创建一个使用指定大小的输入缓冲区的缓冲字符输入流
        而这里面需要的参数Reader是一个抽象方法,不能实例化对象,但是可以用Reader的子类实例化对象
        比如FileReader
        所以:
        BufferedReader bufferedReader = new BufferedReader(new FileReader("文件路径"));

当一个流的构造方法中需要一个流的时候,这个被传进来的流叫做“节点流”,比如上面的new FileReader()
外部负责包装的流叫做包装流,或者处理流,比如new BufferedReader()
而流关闭时,只需要关闭包装流,节点流会自动关闭
成员方法:
    String readLine();  一次读取一行数据,但是不自动换行

//temp.txt文件中保存的内容为:abcdef * 5行
package com.wcc.test;

import java.io.*;

/**
 * @Author kk
 * @Date 2020/3/30 17:02
 */
public class Demo1 {
    public static void main(String[] args) throws IOException {
          /*
        //这是一个输入字节流
        FileInputStream in = new FileInputStream("F:\\Test\\temp.txt");
        //InputStreamReader把字节流转换为字符流
        InputStreamReader isr = new InputStreamReader(in);
        //这个构造方法只能传入一个字符流,不能传入字节流,因此需要上面的方法把字节流转换为字符流
        BufferedReader br = new BufferedReader(isr);
        */
       BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("F:\\Test\\temp.txt")));

        String line = null;
        while((line = br.readLine()) != null){
            System.out.println(line);
        }
        br.close();
    }
}
//输出结果:
// abcdef
// abcdef
// abcdef
// abcdef
// abcdef
//如果用 System.out.print(line);输出,结果为:abcdefabcdefabcdefabcdefabcdef
package com.wcc.test;

import java.io.*;

/**
 * BufferedWriter:带有缓冲的字符输出流
 * 从内存往文件中写入内容(覆盖)
 * @Author kk
 * @Date 2020/3/30 17:12
 */
public class Demo2 {
    public static void main(String[] args) throws Exception {
        BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\Test\\temp.txt"));
        bw.write("hello world!");
        bw.write("\n");
        bw.write("hello U");
        bw.flush();
        bw.close();
    }
}

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值