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=new

FileInputStream("文件路径");
       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);//zhangsan
       System.out.println(password);//123456

ok了家人们明天见。

  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值