Java04-输入输出流

1.输入和输出

1:Output,将字节输出到磁盘。
2:Input,将字节出入到JVM。

2.OutputStream

1:字节输出流,每次只操作文件中的一个字节。
2:可以写任意文件。

3.OutputStream中的方法

1:void write(int b);
2:void write(byte[] b);
3:void write(byte,int start,int length);
4:void close();

4.FileOutputStream

1:文件不存在,可以根据路径创建一个文件,如果存在直接覆盖。
public static void main(String[] args)throws IOException {
    FileOutputStream fos = new FileOutputStream("d:\\demo\\a.txt");//Windows下的分隔符是\,但是\是转移字符,所以写两个\\,表示第一个\将第二个\转为普通的\.

    fos.write(100);//将100转为二进制写入记事本,记事本在打开时,查看二进制表,发现是100转为d。

    fos.close();;
}
byte[] bytes = new byte[]{49,48,48};
fos.write(bytes);//写入100

fos.write("你好啊".getBytes());//字符串转为字节数组直接写入
2:文件的续写。
FileOutputStream(String pathname,boolean append);
FileOutputStream fos = new FileOutputStream("d:\\demo\\a.txt",true);
3:\r\n,换行
fos.write("\r\n".getBytes());

5.IO的异常处理

FileOutputStream fos = null;
try {
    fos = new FileOutputStream("d:\\demo\\a.txt",true);
} catch (IOException e) {
    System.out.println(e.getMessage());
    //系统无法解决 停止程序
    throw new RuntimeException("文件创建失败");
}finally {
    try {
        //只有当fos != null,是才释放资源,
        // FileOutputStream对象创建失败,不需要释放资源
        if (fos != null)
            fos.close();
    } catch (IOException e) {
        System.out.println(e.getMessage());
        //系统无法解决 停止程序
        throw new RuntimeException("资源关闭失败");
    }
}

6.InputStream的方法

1:int read();读取下一个字节,读取到文件结尾时返回-1。
2:int read(byte[] bytes);读取一个字节,并存入byte[],返回值是读取字节的长度,结尾返回-1,数组可以进行缓冲,提高效率。

7.FileInputStream读取文件

FileInputStream fileInputStream = null;
try {
    fileInputStream = new FileInputStream("d:\\demo\\a.txt");

    //使用数组可以起到缓冲的作用
    byte[] bytes = new byte[1024];
    //len 用于存储每次读到的字符数
    //用于将byte[]封装为字符串时,对长度的确定
    int len = 0;
    //如果返回-1 则表示后面没有了字符
    while ((len = fileInputStream.read(bytes)) != -1){
        //可使用String的构造方法将byte[] 封装为字符串
        //new byte[2]; 要读取123
        //则第一次读到的是12
        //第二次读到的是32 所以使用len存储每次读到的字符数,
        //来避免最后一次读取出现 32 这种问题
        String s = new String(bytes,0,len);
        System.out.println(s);
    }
} catch (IOException e) {
    e.printStackTrace();
}finally {
    if (fileInputStream != null){
        try {
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

8.文件的复制

//文件复制
InputStream is = null;
OutputStream os = null;
try {
    is = new FileInputStream("d:\\demo\\a.txt");
    os = new FileOutputStream("d:\\demo\\a1.txt");

    byte[] bytes = new byte[1024];
    int len = 0;
    while ((len = is.read(bytes)) != -1){
        os.write(bytes,0,len);
    }
} catch (IOException e) {
    System.out.println(e.getMessage());
    throw new RuntimeException("文件复制失败");
}finally {
    if (os != null){
        try {
            os.close();
        } catch (IOException e) {
            throw new RuntimeException("关闭os失败");
        }finally {
            //放在finally中,即使关闭os 失败也会执行
            if (is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException("关闭is失败");
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值