当前位置: 首页 > news >正文

JAVA(IO流-字节流)day 7.29

ok家人们今天继续学习IO流,

一.字节流

存储时,都是使用二进制来保存。

2.1 字节输出流OutputStream

 OutputStream是字节输出流的超类(父类),

方法   

public abstract void write(int b):
一次写一个字节数据。public void write(byte[] b):
一次写一个字节数组数据。public void write(byte[] b, int off, int len) :
一次写一个字节数组的部分数据public void close():
关闭此输出流并释放与此流相关联的任何系统资源。

下面是他的子类FileOutputStream。

2.2 FileOutputStream

FileOutputStream 类的概述
文件输出流,用于将数据写入文件中。
FileOutputStream 类的构造方法
public FileOutputStream(File file):创建文件输出流以写入由指定
的 File对象表示的文件。public FileOutputStream(String name): 创建文件输出流以指定
的名称写入文件。FileOutputStream fos = new FileOutputStream('文件地址')//数据写入文件
fos.wirte();//释放资源
fos.close();
public class DemoTest {public static void main(String[] args) throws
Exception {//1.创建流对象FileOutputStream fis= new
FileOutputStream("java_0724_0708\\hello.txt");//2.操作,写字节数组//byte[] bs={97,98,99,100,101};String str="你好";byte[] bytes = str.getBytes();//[-28, -67,
-96, -27, -91, -67]fis.write(bytes);//3.关闭流,释放资源fis.close();}
}
public class DemoTest {public static void main(String[] args) throws
Exception {//1.创建流对象FileOutputStream fis= new
FileOutputStream("java_0724_0708\\hello.txt");//2.操作 写字节数组String str="你好";//[-28, -67, -96, -27, -91, -67]//utf8编码一个中文占3个字节,gbk编码一个中文占2个字
节fis.write(str.getBytes(),0,6);//3.关闭流 释放资源fis.close();}
}

2.3 数据换行与追加

数据追加续写
public FileOutputStream(String name, boolean append):
创建文件输出流以指定的名称写入文件。如果第二个参数为true ,
不会清空文件里面的内容FileOutputStream fos = new FileOutputStream("/相对路径/绝对路径",true);

各个系统的换行符

windows : \r\n
linux : \n
mac : \r

2.4 字节输入流InputStream

InputStream是所有字节输入流的父类,

InputStream构造方法

public abstract int read(): 每次可以读取一个字节的数据,提
升为int类型,读取到文件末尾,返回 -1。public int read(byte[] b): 每次读取b的长度个字节到数组中,
返回读取到的有效字节个数,读取到末尾时,返回 -1。public void close():关闭此输入流并释放与此流相关联的任何系
统资源。

下面是他的子类FileInputStream类。

2.5 FileInputStream

文件输出流,从文件读取数据。

FileInputStream 类的构造方法
FileInputStream(File file): 
通过打开与实际文件的连接来创建一个FileInputStream 
,该文件由文件系统中的文件对象 file命名。FileInputStream(String name): 
通过打开与实际文件的连接来创建一个 FileInputStream ,
该文件由文件系统中的路径名 name命名。FileInputStream fis = new FileInputStream("文件路径");

2.6 读取字节

FileInputStream fis = new FileInputStream("文件路径");fis.read();//每次读取一个字节方法:读取文件所有内容int len;while((len=fis.read())!=-1){System.out.println(len);
}

2.7 读字节数组

FileInputStream fis = new FileInputStream("文件路径");fis.read();//每次读取一个字节方法:读取文件所有内容
byte[] buf = new byte[1024];int len;while((len=fis.read(buf))!=-1){System.out.println(buf,0,len);
}

.字节缓冲流

字节缓冲流 BufferedInputStream BufferedOutputStream
字符缓冲流 BufferedReader BufferedWriter
方法
public BufferedInputStream(InputStream in) :创建一个 新的
缓冲输入流。public BufferedOutputStream(OutputStream out): 创建一个
新的缓冲输出流。
String src = "文件路径";
String dest= "文件路径";
try(BufferedInputStream bis = new BufferedInputStream(src);BufferedOutputStream bos = new BufferedOutputStream(dest);
){//操作}catch (IOException e){e.printStackTrace();}

.Properties集合

4.1 Properties类的概述

它使用键值结构存储数据,继承于 Hashtable

4.2 Properties类的构造方法

public Properties() :创建一个空的属性列表。Properties prop = new Properties();

4.3 Properties类存储方法

public Object setProperty(String key, String value): 
保存一对属性。 public String getProperty(String key) :
使用此属性列表中指定的键搜索属性值。public Set stringPropertyNames() :
获取所有键的名称的集合。Properties prop = new Properties();properties.setProperty("张三", "北京");Set<String> strings = properties.stringPropertyNames();for (String key : strings ) {System.out.println(key+" --
"+properties.getProperty(key));
4.4 Properties 类与流相关的方法
public void load(Reader reader):以字符流形式 , 把文件中的
键值对, 读取到集合中public void store(OutputStream out, String comments):把集
合中的键值对,以字节流形式写入文件中 , 参数二为注释public void store(Writer writer, String comments):把集合中
的键值对,以字符流形式写入文件中 , 参数二为注释
 Properties prop=new Properties();//读取 db.properties属性配置文件FileInputStream fis=newFileInputStream("文件路径");prop. Load(fis);// 关闭流,释放资源fis.close();System.out.println(prop);
Properties prop = new Properties();//存储元素prop.setProperty("name","zhangsan");prop.setProperty("age","18");//把集合中的数据,写入到配置文件FileOutputStream fos=new
FileOutputStream("文件路径");prop. Store(fos,"注释");// 关闭流fos.close();}
}

4.5 ResourceBundle(抽象类)工具类

可以用子类 PropertyResourceBundle 来读取以.properties 结尾的配置文件
ResourceBundle 中常用方法:
String getString(String key) : 通过键,获取对应的值ResourceBundle rb = ResourceBundle.getBundle("##");//不用写后缀名properties
文件username=zhangsan
password=123456
       String username = rb.getString("username");String password = rb.getString("password");System.out.println(username);//zhangsanSystem.out.println(password);//123456

ok了家人们明天见。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • php将数字转为中文汉字
  • 计算机毕业设计LSTM+Tensorflow股票分析预测 基金分析预测 股票爬虫 大数据毕业设计 深度学习 机器学习 数据可视化 人工智能
  • Javascript前端面试基础4【每日学习并更新10】
  • IndexError: list index out of range
  • 物联网架构之Hadoop
  • 区块链软硬件协同,做产业数字化转型的“安全官” |《超话区块链》直播预告
  • 【C++】学习笔记——C++11_1
  • 0729_驱动1 异步通知
  • RocketMQ Server Windows安装
  • 现在有什么赛道可以干到退休?
  • 【音视频之SDL2】Ubuntu编译配置SDL2环境
  • 如何实现无公网IP远程访问本地内网部署的Proxmox VE虚拟机平台
  • 莫斯科国际机场折腾“豪游”的我们
  • mac清理软件哪个好用免费 MacBook电脑清理软件推荐 怎么清理mac
  • 【Java算法专场】二分查找(上)
  • Apache的基本使用
  • CAP理论的例子讲解
  • es6(二):字符串的扩展
  • HTTP 简介
  • HTTP请求重发
  • JavaScript类型识别
  • java概述
  • MySQL用户中的%到底包不包括localhost?
  • nfs客户端进程变D,延伸linux的lock
  • Rancher如何对接Ceph-RBD块存储
  • Spring声明式事务管理之一:五大属性分析
  • V4L2视频输入框架概述
  • 记录一下第一次使用npm
  • 解析带emoji和链接的聊天系统消息
  • 聚类分析——Kmeans
  • 每个JavaScript开发人员应阅读的书【1】 - JavaScript: The Good Parts
  • 区块链分支循环
  • 为物联网而生:高性能时间序列数据库HiTSDB商业化首发!
  • 自动记录MySQL慢查询快照脚本
  • 阿里云服务器如何修改远程端口?
  • 策略 : 一文教你成为人工智能(AI)领域专家
  • ​ ​Redis(五)主从复制:主从模式介绍、配置、拓扑(一主一从结构、一主多从结构、树形主从结构)、原理(复制过程、​​​​​​​数据同步psync)、总结
  • ‌分布式计算技术与复杂算法优化:‌现代数据处理的基石
  • # SpringBoot 如何让指定的Bean先加载
  • #我与Java虚拟机的故事#连载10: 如何在阿里、腾讯、百度、及字节跳动等公司面试中脱颖而出...
  • $HTTP_POST_VARS['']和$_POST['']的区别
  • (17)Hive ——MR任务的map与reduce个数由什么决定?
  • (二)基于wpr_simulation 的Ros机器人运动控制,gazebo仿真
  • (十三)Flask之特殊装饰器详解
  • (转)我也是一只IT小小鸟
  • ***详解账号泄露:全球约1亿用户已泄露
  • .gitignore不生效的解决方案
  • .NET精简框架的“无法找到资源程序集”异常释疑
  • .Net开发笔记(二十)创建一个需要授权的第三方组件
  • @PreAuthorize注解
  • @RequestMapping 的作用是什么?
  • @staticmethod和@classmethod的作用与区别
  • [ 蓝桥杯Web真题 ]-Markdown 文档解析
  • [100天算法】-二叉树剪枝(day 48)
  • [20171102]视图v$session中process字段含义