IO流的常用方法(三)

对象序列化

对象序列化:将对象转换为二进制字节序列并储存在存储介质中的过程。
注意: 对象序列化和反序列化的前提是本类或其超类实现Serializable接口。

ObjectOutputStream

       //创建ObjectOutputStream对象
		FileOutputStream fos = new FileOutputStream("src/a.txt");
	
	    ObjectOutputStream oos = new ObjectOutputStream(fos);
	    
	    //写入磁盘中
	    Student student = new Student(1,"浩哥","男");
	    oos.writeObject(student);
	    //先开的后关
	    oos.close();
	    fos.close();

反对象序列化

反对象序列化读取存储介质中的二进制字节序列并转换成对象的过程。
注意: 对象序列化和反序列化的前提是本类或其超类实现Serializable接口。

ObjectInputStream

         //创建ObjectInputStream对象
	     FileInputStream fileInputStream = new FileInputStream("src/a.txt");
		//写入磁盘中
         ObjectInputStream ois = new ObjectInputStream(fileInputStream);
		
         Student readObject = (Student) ois.readObject();
         System.out.println(readObject);
         //先开的后关
	    ois.close();
	    fileInputStream.close();

Properties类

向配置文件中写入配置信息的步骤

        //创建FileOutputStream对象
        FileOutputStream fileOutputStream = new FileOutputStream("src/user.properties");
		//创建Properties对象
        Properties properties = new Properties();
		//通过Properties对象的setProperty方法设置键值对信息
        properties.setProperty("username", "lihao");
        properties.setProperty("password", "123456");
		//通过Properties对象的store方法将配置信息写入磁盘文件
        properties.store(fileOutputStream, "first");
		//关闭流
		fileOutputStream.close();

从配置文件中读取配置信息的步骤

        //创建FileInputStream对象
        FileInputStream fis = new FileInputStream("src/user.properties");
        //创建Properties对象
        Properties properties = new Properties();
        //通过Properties对象的load方法加载配置文件中的内容
        properties.load(fis);
        //通过Properties对象的get方法获得配置信息
        String str ="username";
        String value = properties.getProperty(str);
        System.out.println(str+"--------------"+value);
        //关流
        fis.close();

RandomAccessFile类

RandomAccessFile类:是可以访问文件中任意位置内容的类

RandomAccessFile类读取文件内容并打印输出的操作步骤

       //以读模式创建RandomAccessFile对象
        File file = new File("src/a.txt");
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
        //移动文件指针到文件内容的中间位置
        randomAccessFile.seek(file.length()/2);
		//通过read方法读取文件内容
        byte[] b= new byte[1024];
		int i=0;
        while((i=randomAccessFile.read(b))!=-1) {
        	System.out.println(new String(b,0,i));
        }
		//关闭流
        randomAccessFile.close();

IO的分类

阻塞IO是程序发起IO请求便只能等待直到获得响应结果的IO
非阻塞IO是程序发起IO请求便可进行其他操作的IO
同步IO是程序依赖IO操作返回的结果才能得以继续执行的IO
异步IO是程序并不依赖IO操作返回的结果便可以继续执行并会得到IO反馈通知的IO
在这里插入图片描述
在这里插入图片描述

Charset类

作用: 操作字符集
常用方法
在这里插入图片描述

FileChannel类和Buffer类

FileChannel类 是实现了Channel接口,并用于文件读写的类
Buffer类 是存储数据并与通道Channel进行数据交互的类
Buffer类的常用方法
在这里插入图片描述
FileChannel类的常用方法
在这里插入图片描述
注意:
用NIO进行文件操作时,需要先获取FileChannel。(NIO是面向缓冲区,非阻塞,同步的IO。)
只能通过FileInputStream,FileOutputStream,RandomAccessFile 来获取FileChannel。

使用FileChannel+CharBuffer写入磁盘文件

       //获取FileChannel对象
		FileOutputStream fos = new FileOutputStream("src/a.txt");
		FileChannel fc = fos.getChannel();
		//为Buffer缓冲区分配空间,向Buffer中写入数据,并切换Buffer为读模式
		CharBuffer buffer = CharBuffer.allocate(1024);//为缓冲区分配空间 
		buffer.put("你好 Java");//向缓冲区中写入字符串
		buffer.flip();//将缓冲区指针移动到首位
		//设置字符集编码格式,对CharBuffer对象进行编码
		Charset charset = Charset.forName("UTF-8");//设置字符集编码格式
		ByteBuffer buffer2 = charset.encode(buffer);//对CharBuffer对象进行编码
		fc.write(buffer2);//将编码后的缓冲区内容通过文件通道写入磁盘文件
        //关闭流
		fc.close();
		fos.close();

使用FileChannel+CharBuffer读取磁盘文件

        //获取FileChannel对象
		File file=new File("src/a.txt");
		FileInputStream fis = new FileInputStream("src/a.txt");
		FileChannel fc = fis.getChannel();
        //通过fc的map方法映射文件内容到内存中
		ByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0,  file.length());
        //设置字符集编码格式,对ByteBuffer缓冲区内容进行解码
		Charset charset = Charset.forName("UTF-8");
		CharBuffer cb = charset.decode(buffer);
		System.out.println(cb.toString());
        //关闭流
		fc.close();
		fis.close();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值