Java IO流 节点流

IO流 节点流
从/向一个特定的IO设备(如磁盘、网络)读/写数据的流,称为节点流,也叫低级流。节点流进行输入/输出时,程序是直接连接到实际的数据源,和实际的输入节点连接。
在这里插入图片描述

节点流通过是对字符还是字节操作细分为字节流、字符流。

1、字节流
在这里插入图片描述
实例
写入文件

/**
功能: 文件输出流
IO流的操作步骤:
1.创建一个File类,要操作的文件
2.创建一个输出或者输入流
3.进行读或者写
4.关闭资源
*/
public class FileOutputStreamDemo1 {
    public static void main(String[] args) throws Exception {
    //1.创建一个File类
    File file = new File("C:\Users\Administrator\Desktop\aa.txt");
    //2.创建一个流
    OutputStream os = new FileOutputStream(file,true);    //ture表示写入是追加内容,默认时覆盖
    //3.写数据
    os.write("你好世界helloworld123123123123\r".getBytes());
    //4.关闭资源
    os.close();
    }
}

读取文件

//循环读取文件
public static void readFile3() throws FileNotFoundException, IOException {
    InputStream is = new FileInputStream(new File("C:\Users\Administrator\Desktop\aa.txt"));
    // 读取文件
    // 创建一个数组byte类型的数组,用类存放数据
    byte[] by = new byte[1024];// file.length()读取文件大小
    // 读取数据
    int len = 0;
    while ((len = is.read(by)) != -1) {
        String str = new String(by, 0, len);
        System.out.print(str);
    }
    is.close();
}

2、字符流
在这里插入图片描述

实例
写入文件

public static void writerFile() throws IOException {
	// 1.创建一个字符流对象
	Writer writer = new FileWriter("src/com/yueqian/字符流/aa.txt", true);
	// 2.写数据
	writer.write("helloworldwewreewrt\r");
	// 3.关闭资源
	writer.close();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值