Java中的四大流——字节流、字符流

字符流

字节流也是可以读取文本文件,但是可能会出现读取中文时,只读取其中的部分字节,因为中文不止由一个字节组成。所以引入字符流。

字符输入流(Reader)

顶层父类:Reader(抽象类)
共性方法:

public void close();//释放资源
public int char read();//一次读一个字符,返回的值码值
public int read (char[] chs);//一次读取一个字符数组,返回值表示实际读取的字符个数

FileReader类

文件的字符输入流,从文件中读取以字符为单位的数据。

构造方法

public FileRead(String path)
public FileRead(File file)

读取字符数据

1、一次读取一个字符数据

    public static void main(String[] args) throws IOException {
        //1、创建一个filereader对象
        FileReader ff = new FileReader("1.txt");
        /*
         * 创建对象
         * 判断文件是否存在:
         * 如果存在,不清空!!!
         * 如果不存在,则报错。
         * 绑定ff和1.txt
         * */
        //2、一次读取一个字符
        int read = ff.read();
        System.out.println((char) read);
        //标准循环格式
        int len = 0;//用于存储读取到的数据
        while ((len = ff.read()) != -1) {
            System.out.println((char) len);
        }

2、一次读取一个字符数组

        //2、一次读取一个字符数组
        //char[]chars=new char[2];
        //int read = ff.read(chars);
        //System.out.println(new String (chars));
        //标准循环格式
        char[] chars = new char[2];//保存字符数据的数组
        int len = 0;//保存实际读取的个数
        while ((len = ff.read(chars)) != -1) {
            System.out.println(new String(chars, 0, len));
        }
        //释放资源
        ff.close();

字符输出流(Writer)

顶层父类:Writer(抽象类)
共性方法:

public void close();//释放资源
public void flush();//刷新
public void writer(int ch);//一次读一个字符,返回的值码值
public void writer (char[] chs);//一次读取一个字符数组,返回值表示实际读取的字符个数
public void writer (char[] chs,int startIndex,in len);//直接写一个字符数组的一部分
public void writer(String str);//直接写一个字符串
public void writer (String str,int startIndex,in len);//写出一个字符串的一部分

FileWriter类

文件袋字符输出流(向文件中写数据)

构造方法

public FileRead(String path)
public FileRead(File file)

基本写出数据

    public static void main(String[] args) throws IOException {
        FileWriter ff = new FileWriter("2.txt");
        /*
         * 创建对象
         * 判断文件是否存在:
         * 如果存在,清空!!!
         * 如果不存在,创建。
         * 绑定ff和2.txt
         * */
        //写一个字符
        ff.write('a');
        //写一个字符数组
        char[] chars = {'a', 'b'};
        ff.write(chars);
        //写一个字符数组的一部分开始索引,个数
        ff.write(chars, 3, 2);
        //直接写一个字符串
        ff.write("woaijava");
        //直接写一个字符串的一部分
        ff.write(chars, 2, 5);
        //资源释放
        ff.close();
    }
}

关闭和刷新

flush():刷新缓冲区,不关闭流
close():不仅会刷新缓冲区,而且会关闭流

    public static void main(String[] args) throws IOException {
        FileWriter ff = new FileWriter("3.txt");
        ff.write("php");//数据刚开始是被写入了缓冲区,调用flush方法,将缓冲区的数据写入文件中
        //刷新缓冲区
        ff.flush();
        //再次写数据
        ff.write("java");
        ff.flush();
        //关闭资源
        ff.close();
    }
}

续写和换行

public FileWriter()
    public static void main(String[] args) throws IOException {
        FileWriter ff = new FileWriter("4.txt", true);
        //续写
        ff.write("h5");
        //换行,写一个换行符即可
        ff.write("\r\n");
        ff.write("python");
        ff.write("\r\nc++");
        //关闭资源
        ff.close();
    }
}

IO资源的处理

JDK1.7前的处理

    public static void main(String[] args) {
        FileReader ff = null;
        try {
            //创建对象
            ff = new FileReader("4.txt");
            //读
            int read = ff.read();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            try {
                if (ff == null) {
                    ff.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

JDK1.7后的处理

   public static void main(String[] args) {
        try(FileReader ff = new FileReader("4.txt")) {
            //读
            int read = ff.read();
        }catch (IOException E){
            E.printStackTrace();
        }
    }
}

属性集

概述

继承与Hshtable,而Dictionary,实现了Map接口。

Properties类

Properties就是一个双列集合。使用键值结构存数据,然而,每个键及其对应的值已经确定为String了

构造方法

public Properties();//创建一个空的Properties对象
    public static void main(String[] args) {
        ///创建一个空的Properties对象
        Properties pp = new Properties();
        System.out.println(pp);//{}
    }
}

基本的存储方法

Properties是map的实现类,所以具有map中的方法。(增删改查、遍历)
特有方法:

public Object setProperty(String key,String value);//添加/修改键值对
public Object getProperty(String key);//以键找值
public Set<String> stringPropertyNames();//获取所有属性名的集合,和KeySet一样
    public static void main(String[] args) {
        ///创建一个空的Properties对象
        Properties pp = new Properties();
        System.out.println(pp);//{}
        //添加
        pp.setProperty("小米", "3000");
        pp.setProperty("苹果", "8000");
        pp.setProperty("华为", "9000");
        System.out.println(pp);
        //修改
        pp.setProperty("小米", "4000");
        //获取
        String string = pp.getProperty("苹果");
        System.out.println(string);
        //获取所有属性名的集合
        Set<String> strings = pp.stringPropertyNames();
        System.out.println(strings);
    }
}

和流相关的方法

Properties有两个和流相关的方法:加载和保存
保存:

 public void store(OutputStream out,String 注释)//用于保存Properties对象的数据
 public void store(Writer writer,String 注释)//用于保存Properties对象的数据
    public static void main(String[] args) throws IOException {
        ///创建一个空的Properties对象
        Properties pp = new Properties();
        System.out.println(pp);//{}
        //添加
        pp.setProperty("user", "root");
        pp.setProperty("ps", "123");
        //保存,只能是输出流,写流【规范,文件名建议使用Properties作为后缀】
        pp.store(new FileOutputStream("4.txt"),"");//""是写注释用的
        //读取
    }
}

加载:

  public void load(InputStream in)//用于加载Properties对象的数据
  public void load(Reader r)//用于加载Properties对象的数据
    public static void main(String[] args) throws IOException {
        ///创建一个空的Properties对象
        Properties pp = new Properties();
        pp.load(new FileInputStream("4.txt"));
        System.out.println(pp);
    }
}

ResourceBundle工具类

概述

ResourceBundle实际上是一个抽象类,他的子类是PropertyResourceBundle,可以读取以Properties为后缀的文件中的内容。

ResourceBundle类对象的创建

public static ResourceBundle getBundle(String baseName);//用于绑定指定的.Properties资源文件。

1、必须放到类的根路径下(src文件夹下)
2、给定参数只需要配置文件的名称,不需要后缀。

ResourceBundle读取配置文件的操作

public static String getString (String key);//读取配置文件
    public static void main(String[] args) {
        //创建
        ResourceBundle data = ResourceBundle.getBundle("data");
        System.out.println(data);
        String user = data.getString("user");
        System.out.println(user);
    }
}

小结

关于流的问题,是面试中经常会被问到的,所以必须要知道哦!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值