day23IO流(字节流&字符流&操作基本数据类型的流&序列化流)&Properties集合

这篇博客详细介绍了Java中的IO流,包括字节流(InputStream和OutputStream)、字符流(Reader和Writer)及其缓冲流,还有操作基本数据类型的DataInputStream和DataOutputStream。此外,还讨论了序列化流(ObjectOutputStream和ObjectInputStream)以及Properties集合的使用,如setProperty和getProperty方法。
摘要由CSDN通过智能技术生成

IO:指的是设备之间进行数据传入的流
按照流向分类:
    输入流
    输出流
按照数据类型分类:
    字节流
        A:字节输入流 InputStream
            FileInputStream:
            FileInputStream fis = new FileInputStream("读取数据的文件名");
            字节缓冲输入流:BufferedInputStream
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream("读取数据的文件名"));
            //注意:这里的文件必须提前存在
        字节输入流读取数据两种方式:
            1、一次读取一个字节
            int b = 0;
            while((b=bis.read())!=-1){
                System.out.print((char)b);
            }
            bis.close();

            2、一次读取一个字节数组
            byte[] bytes = new byte[1024];
            int length = 0;
            while((length = bis.read(bytes))!=-1){
                System.out.print(new String(bytes,0,length));
            }
            bis.close();

package com.shujia.wyh.day23;


import java.io.BufferedInputStream;
import java.io.FileInputStream;

public class BufferedInputStreamDemo1 {
    public static void main(String[] args) throws Exception {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("g.txt"));

        //读取数据
        //一次读取一个字节
//        int b = 0;
//        while ((b=bis.read())!=-1){
//            System.out.print((char) b);
//        }

        System.out.println("======================");

        byte[] bytes = new byte[1024];
        int length = 0;
        while ((length = bis.read(bytes)) != -1) { //bis.read(bytes)调用这个方法,将实际获取到的字节存储到数组中,返回的是数组实际读取到的字节数
            System.out.println(new String(bytes, 0, length));
        }

        //释放资源
        bis.close();


    }

}

        B: 字节输出流 OutputStream
            FileOutputStream:
                FileOutputStream fos = new FileOutputStream("写文件的名字");
            字节缓冲输出流:BufferedOutputStream
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(写文件的名字));
            //注意:这里文件,可以不存在,之后会自动创建
        写数据的方式:bos.write()
                1、一次写一个字节
                2、一次写一个字节数组
                3、一次写一个字节数组的一部分

package com.shujia.wyh.day23;
import java.io.Buffered
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值