day23

Properties集合

特点:

  1. 该集合中的键和值都是字符串类型
  2. 集合中的数据可以保存到流中,或者从流获取
  3. 通常该集合用于操作以键值对形式存在的配置文件
  4. extends Hashtable
基本功能
存取
Properties pro = new Properties();
pro.setProperty("sss","11");
pro.setProperty("aaa","22");
pro.setProperty("vvv","33");
Set<String> names = pro.stringPropertyNames();
for(String name : names){
  String value = pro.getProperty(name);
  System.out.println(name+":"+value);
}
修改
pro.setProperty("sss","23");
store方法
//想要将这些集合中的字符串键值对信息存储到文件中,需要关联输出流
FileOutputStream fos = new FileOutputStream("info.txt");
pro.store(fos,"info");
fos.close();
修改配置信息
  1. 读取配置文件
  2. 将文件中的键值对数据存储到集合中
  3. 再通过集合对数据进行修改
  4. 再通过流将修改后的数据存储到文件中
//读取已有文件的配置信息,需要用到读取流
File file = new File("info.txt");
FileReader fr = new FileReader(file);
//创建集合存储配置信息
Properties pro = new Properties();
//将信息存到集合中
pro.load(fr);
pro.setProperty("aaa","18");//修改信息
//新建一个输出流,重新建立info.txt文件,重新写入修改后配置文件的所有信息
FileWriter fw = new FileWriter(file);//这行代码的顺序不能提前
pro.store(fw,"info");
fw.close();
fr.close();

IO包中的其他流

PrintStream
  1. 提供了打印方法可以对多种数据类型值进行打印,并保持数据的表示形式
  2. 不会抛出IOException
  3. 构造函数接收三种类型的值:字符串路径,File对象,字节输出流
PrintStream out = new PrintStream("print.txt");
out.write(97);//文件里为a
out.write(610);//文件里为b,因为write只写入最低8位
PrintStream out = new PrintStream("print1.txt");
out.print(97);//将97先变成字符将数据打印到目的地,所以print1文件里是97
out.close();
PrintWriter
  • 构造函数接收四种类型的值:字符串路径,File对象,字节输出流,字符输出流
序列流
合并文件
//用于流的合并
ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
al.add(new FileInputStream("1.txt"));
al.add(new FileInputStream("2.txt"));
al.add(new FileInputStream("3.txt"));
Enumeration<FileInputStream> en = Collections.enumeration(al);//枚举
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream("4.txt");
byte[] buf = new byte[1024];
int len = 0;
while((len=sis.read(buf))!=-1){
  fos.write(buf,0,len);
}
fos.close();
sis.close();
切割文件
public static void splitFile(File file) throws IOException{
  //用读取流关联源文件
  FileInputStream fis = new FileInputStream(file);
  //定义一个1M的缓冲区
  byte[] buf = new byte[1024*1024];
  //创建目的
  FileOutputStream fos =null;
  int len = 0;
  int count = 1;
  File dir = new File("c:\\partfiles");
  if(!dir.exists())
    dir.mkdirs();
  while((len=fis.read(buf))!=-1){
    fos = new FileOutputStream(new File(dir,(count++)+".part"));
    fos.write(buf,0,len);
  }
  fos.close();
  fis.close();
}
对象的序列化
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.object"));
oos.writeObjcet(new Person("fxl",18));
oos.close();
//public class Person implements Serializable{} 类要实现此接口,类的对象才能序列化
ObjectIntputStream只能对用ObjectOutputStream写入的对象反序列化
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.object"));
Person p = (Person)ois.readObject();
ois.close();
//Serializable接口:用于给被序列化的类加入ID号,以此来判断类和对象是否为同一版本,需要在对象所属的类声明ID
private static final long seriaVersionUID = 3232l;

tips:

  • 将指定对象写入ObjectOutputStream,类中瞬态和静态字段的值时不会写入进去的
  • 瞬态关键字:transient
RandomAccessFile
  • 不是IO包中的子类
  • 该对象能读能写
  • 对象内部维护一个byte数组,并可以操作数组元素
  • 通过getFilePointer获取指针位置,通过seek设置指针位置
  • 其实该对象就是将字节输入流和输出流进行了封装
  • 可以通过seek实现多个线程往一个文件写入数据
    在这里插入图片描述
    写入
RandomAccessFile raf = new RandomAccessFile("ran.txt","rw");
//raf.seek(3*8);可以实现往指定位置写入数据,指针默认从0开始
raf.write("lzh".getBytes());//字符流变字节流
raf.writeInt(609);//四个字节
//raf.write(609);只写入最后八个字节
raf.close();

读取

RandomAccessFile raf = new RandomAccessFile("ran.txt","r");
//raf.seek(1*8);//输出变为小强:99,指针指向8
byte[] buf = new byte[4];
raf.read(buf);
String name = new String(buf);
int age = raf.readInt();//readInt()一次读取四个字节并转换成整数
System.out.println(name+":"+age);//张三:97
raf.close();
其他流对象
  1. 操作基本数据类型的流对象:DataInputStream,DataOutputStream,关闭流无效
  2. 操作字节数组:ByteArrayOutputStream,实现了一个输出流,其中的数据被写入一个byte数组,缓冲区会随着数据写入增长,可使用toByteArray(),toString()获取数据,关闭流无效,关闭后仍可调用
ByteArrayInputStream bis = new ByteArrayInputStream("adsfd".getBytes());
ByteArrayOutputStream bos = new ByteArrayOutputStream();//源和目的都是数组
int ch =0;
while((ch=bis.read())!=-1){
  bos.write(ch);
}//不用关闭流
System.out.println(bos.toString());
  1. 操作字符数组:CharArrayReader,CharArrayWriter
  2. 操作字符串:StringReader,StringWriter
编解码

编码:字符串转换为字节数组

解码:字节数组转换为字符串

String str = "你好";
byte[] buf = str.getBytes("utf-8");//编码
String s1 = new String(buf,"utf-8");//解码

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值