IO流相关应用

1.1.字节流复制文件
//运用字节输入输出流将a.txt文件复制到b.txt中
public class Demo1 {
    public static void main(String[] args) throws IOException {
        //创建输入流对象
        FileInputStream fis = new FileInputStream("day11_myIO\\a.txt");
        //可以使用BufferedInputStream的子类对象进行包装,可以提高读取数据的效率(底存为数组)
        BufferedInputStream bis = new BufferedInputStream(fis);
        //创建输出流对象
        FileOutputStream fos = new FileOutputStream("day11_myIO\\b.txt");
        //可以使用BufferedOutputStream的子类对象进行包装,可以提高读取数据的效率(底存为数组)
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //读取a.txt
        byte[] bytes = new byte[1024];
        int len =bis.read(bytes);//记录读取字节的个数
        while (len!=-1){
            //一边读取,一边写入
            bos.write(bytes,0,len);
            //System.out.println(len);
            //写完再读
            len =bis.read();
        }
        //释放资源
        bos.close();
        bis.close();
    }
}

1.2.字节流捕获异常
//字节流输出写入扑获异常
public class Demo2 {
    public static void main(String[] args) {
        FileOutputStream fos = null;
        try {
            //创建流对象
            fos = new FileOutputStream("day11_myIO\\a.txt");
            //写数据
            fos.write("hello".getBytes());

        } catch (IOException e) {
            e.printStackTrace();//如果异常,则抛出打印到控制台
        } finally {//不管异常是否发生,finally里面的代码一定会执行(释放资源的代码)
            if (fos != null) {//如果创建对象失败,则会为null
                try {         //判断是否为空,避免空指针异常
                    //释放资源
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

1.3.字节输入流读取数据并打印至控制台
//字节流InPutStream为字节流读数据抽象类
//具体使用它的子类FileInputStream【输入流】
public class Demo1 {
    public static void main(String[] args) throws IOException {
        //构造方法,参数为File对象
        //FileInputStream fs = new FileInputStream(new File("day11_myIO\\a.txt"));
        //构造方法,参数为文件路径
        //1.创建输入流
        FileInputStream fis = new FileInputStream("day11_myIoByteStream\\a.txt");
        //2.一次读一个字节
         /*int b = fis.read(); // 返回的则为字节对应的值
         while (b!=-1){
             System.out.print((char)b);
             b = fs.read();a.txt
         }*/
         //一次读多个字节
        byte[] bytes = new byte[1024];
        int len = fis.read(bytes);
        while (len!=-1){//返回-1则读完
            String str = new String(bytes, 0, len);
            System.out.println(str);
            len = fis.read(bytes);
        }
        //3.释放资源(关流)
        fis.close();
    }
}
打印结果:
------------------------------------------------------------------------------
hello
world
java
2.1.字符流复制文件
//BufferedReader,BufferedWriter字符缓冲输入输出流
//特有方法:bw.newLine();换行符[跨平台性];br.readLine();读取一行数据
public class Demo1 {
    public static void main(String[] args) throws IOException {
        //1.创建对象
        //创建BufferedReader对象对FileReader进行包装
        BufferedReader br = new BufferedReader(new FileReader("day12_myIoCharStream\\a.txt"));
        //创建BufferedWriter对象对FileWriter进行包装
        BufferedWriter bw = new BufferedWriter(new FileWriter("day12_myIoCharStream\\b.txt"));
        //2.读数据
        String line = br.readLine();//读取到返回一行的字符串
        while (line!=null){
            //line为读到的每一行数据
            //边读边写
            bw.write(line);
            bw.newLine();
            bw.flush();
            line = br.readLine();
        }
        bw.close();
        br.close();
    }
}

2.2.字符流读取文本学生信息,操作后再写入文本
public class Demo2 {
    public static void main(String[] args) throws IOException {
        //1.读取
       //创建字符输入流对象
        BufferedReader br = new BufferedReader(new FileReader("day12_myIoCharStream\\c.txt"));
        //2.字符缓冲输入流读取文本
        ArrayList<Student> list = new ArrayList<Student>();
        String line = br.readLine();
        while (line!=null){
            //3.将读取的信息切割封装为学生对象存入集合中
            String[] strArr = line.split(",");
            list.add(new Student(strArr[0],Integer.parseInt(strArr[1])));
            line = br.readLine();
        }
        //循环结束,数据全部存入集合,释放资源
        br.close();
        //4.将集合安装学生年龄排序
        Collections.sort(list,(o1, o2) -> o1.getAge()-o2.getAge());
        //System.out.println(list);
        //5.排序完毕,创建字符输出流写入文本
        BufferedWriter bw = new BufferedWriter(new FileWriter("day12_myIoCharStream\\d.txt"));
        //6.遍历集合,得到每一个学生对象,写入文本
        for (Student student : list) {
            StringBuilder sb = new StringBuilder();
            bw.write(sb.append(student.getName()).append(",").append(student.getAge()).toString());
            //bw.write(student.getName()+","+student.getAge());
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }
}

2.3.字符串解码
//字符串解编码
public class Demo1 {
    public static void main(String[] args) throws UnsupportedEncodingException {
        //编码:把字符串转化为字节数组[一个汉字UTF-8默认三个字节,GBK中是二个字节]
        byte[] byte1 = "你好世界".getBytes("gbk");
        System.out.println(Arrays.toString(byte1));
        //解码
        //解码使用的编码表必须和编码保持一致
        //通过使用平台的默认字符集解码指定的字节数组来构造新的String
        String s1 = new String(byte1);//由于idea默认为utf-8,则打印出来乱码
        //通过指定的字符集解码指定的字节数组来构造新的String
        String s2 = new String(byte1, "gbk");
        System.out.println(s1);
        System.out.println(s2);
    }
}

打印结果:
-------------------------------------------------------------------------------
[-60, -29, -70, -61, -54, -64, -67, -25]
�������
你好世界
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陪雨岁岁年年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值