您当前的位置: 首页 >  Java

ITKEY_

暂无认证

  • 0浏览

    0关注

    732博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Java温故而知新-数据流

ITKEY_ 发布时间:2021-01-27 21:33:12 ,浏览量:0

文章目录
  • 数据流简介
  • OutputStream
    • 简介
    • 常用方法
    • FileOutputStream子类
    • 实现数据的追加
  • InputStream字节输入流
    • 简介
    • 常用方法
    • 数据读取

数据流简介

  在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

OutputStream 简介

在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

常用方法

在这里插入图片描述

FileOutputStream子类

文件输出

import java.io.*;

public class YootkDemo {    // 李兴华编程训练营:yootk.ke.qq.com
    public static void main(String[] args) throws Exception {
        File file = new File("H:" + File.separator + "muyan" + File.separator + "vip" + File.separator + "yootk.txt") ;
        if (!file.getParentFile().exists()) {   // 此时文件有父目录
            file.getParentFile().mkdirs() ; // 创建父目录
        }
        OutputStream output = new FileOutputStream(file) ; // 实例化OutputStream抽象类对象
        String message = "www.yootk.com" ; // 此为要输出的数据内容
        // OutputStream类的输出是以字节数据类型为主的,所以需要将字符串转为字节数据类型
        byte data [] = message.getBytes() ; // 将字符串转为字节数句
        output.write(data); // 输出全部字节数组的内容
        output.close(); // 关闭输出流
    }
}

在这里插入图片描述

实现数据的追加
import java.io.*;

public class YootkDemo {    // 李兴华编程训练营:yootk.ke.qq.com
    public static void main(String[] args) throws Exception {
        File file = new File("H:" + File.separator + "muyan" + File.separator + "vip" + File.separator + "yootk.txt") ;
        if (!file.getParentFile().exists()) {   // 此时文件有父目录
            file.getParentFile().mkdirs() ; // 创建父目录
        }
        OutputStream output = new FileOutputStream(file, true) ; // 实例化OutputStream抽象类对象
        String message = "www.yootk.com\r\n" ; // 此为要输出的数据内容
        // OutputStream类的输出是以字节数据类型为主的,所以需要将字符串转为字节数据类型
        byte data [] = message.getBytes() ; // 将字符串转为字节数句
        output.write(data); // 输出全部字节数组的内容
        output.close(); // 关闭输出流
    }
}
InputStream字节输入流 简介

在这里插入图片描述

常用方法

在这里插入图片描述 在这里插入图片描述

数据读取

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class YootkDemo {    // 李兴华编程训练营:yootk.ke.qq.com
    public static void main(String[] args) throws Exception {
        File file = new File("H:" + File.separator + "muyan" + File.separator + "vip" + File.separator + "yootk.txt");
        if (file.exists()) {    // 文件存在
            try (InputStream input = new FileInputStream(file)) {
                byte data[] = new byte[1024]; // 开辟1K的空间进行读取
                int len = input.read(data); // 读取数据到字节数组并返回读取个数
                System.out.println("读取到的数据内容【" + new String(data, 0, len) + "】");
            } catch (Exception e) {}
        }
    }
}
关注
打赏
1665243900
查看更多评论
立即登录/注册

微信扫码登录

0.0647s