Lesson_for_java_day18--java中的IO流(序列化、ByteArrayStream、DataStream、RandowAccessFile)

本文主要是介绍Lesson_for_java_day18--java中的IO流(序列化、ByteArrayStream、DataStream、RandowAccessFile),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、序列化:

package sonyi;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;public class SerializeDemo {public static void main(String[] args) {// TODO Auto-generated method stubwrite();//写read();//读}//反序列化public static void read(){ObjectInputStream ois = null;try {ois = new ObjectInputStream(new FileInputStream("obj.txt"));ArrayList<Person> arr = (ArrayList<Person>)ois.readObject();for(Person p:arr){//按顺序读取对象System.out.println(p);}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {if(ois != null)ois.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}//序列化public static void write(){ObjectOutputStream oos = null;ArrayList<Person> arr = new ArrayList<Person>();//创建容器装对象try {oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));arr.add(new Person("zhangsan", 25));arr.add(new Person("lisi", 26));arr.add(new Person("wangwu", 27));oos.writeObject(arr);//将容器作为对象传递给流	//System.out.println(oos);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {if(oos != null)oos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}class Person implements Serializable{private String name;private int age;Person(String name,int age){this.name = name;this.age = age;}@Overridepublic String toString() {return "Person [name=" + name + ", age=" + age + "]";}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}

二、ByteArrayStream

package io;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;/*用于操作字节数组的流对象:ByteArrayInputStream:在构造的时候,需要接收数据源,而且数据源是一个字节数组ByteArrayOutputStream:在构造的时候,不用定义数据目的,因为该对象中已经内部封装了可变长度的字节数组这就是数据目的地。因为这两个流对象都操作的数组,并没有使用系统资源,所以不用进行close关闭。在流操作的讲解时:源设备:键盘(System.in)、硬盘(FileStream)、内存(ArrayStream)目的设备:控制台(System.out)、硬盘(FileStream)、内存(ArrayStream)用流的读写思想来操作数组。*/
public class ByteArrayStream {public static void main(String[] args) {// TODO Auto-generated method stub//数据源ByteArrayInputStream bis = new ByteArrayInputStream("abdcefg".getBytes());//数据目的ByteArrayOutputStream bos = new ByteArrayOutputStream();int by = 0;while((by = bis.read()) != -1){bos.write(by);}System.out.println(bos.size());System.out.println(bos.toString());//bos.writeTo(new FileOutputStream("a.txt"));}
}

三、DataStream

package io;import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;/*基本数据类型流对象:DataInputStream与DataOutputStream:可以用于操作基本数据类型的数据的流对象*/
public class DataStreamDemo {public static void main(String[] args) {
//		writeData();
//		readData();writeUTFDemo();readUTFDemo();}public static void readUTFDemo(){DataInputStream dis = null;try {dis = new DataInputStream(new FileInputStream("utfdata.txt"));String s = dis.readUTF();System.out.println(s);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {if(dis != null)dis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void writeUTFDemo(){DataOutputStream dos = null;try {dos = new DataOutputStream(new FileOutputStream("utfdata.txt"));dos.writeUTF("你好");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {if(dos != null)dos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void readData(){DataInputStream dis = null;try {dis = new DataInputStream(new FileInputStream("data.txt"));int num = dis.readInt();boolean b = dis.readBoolean();double d = dis.readDouble();System.out.println("num = " + num);System.out.println("b = " + b);System.out.println("d = " + d);			} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {if(dis != null)dis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void writeData(){DataOutputStream dos = null;try {dos = new DataOutputStream(new FileOutputStream("data.txt"));dos.writeInt(124);dos.writeBoolean(true);dos.writeDouble(9887.123);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {if(dos != null)dos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}

四、RandowAccessFile

package io;import java.io.RandomAccessFile;
/*RandowAccessFile:(可以实现多线程对文件的同时操作。比如下载)随机访问文件该类不算是IO体系中子类,而是直接继承自Object。但是它是IO包中成员,因为它具备读和写功能,内部封装了一个数组,而且通过指针对数组的元素进行操作,可以通过getFilePointer获取指针位置。同时可以通过seek改变指针的位置。其实完成读写的原理就是内部封装了字节输入流和输出流。通过构造函数可以看出,该类自能操作文件。而且操作文件还有模式:只读r,读写rw等。如果模式为只读,不会创建文件,会去读取一个已存在的文件,如果该文件不存在,则会出现异常如果模式为读写,那么该对象的构造函数要操作的文件如果不存在,会自动创建,如果存在,不会覆盖*/
public class RandowAccessFileDemo {public static void main(String[] args){try {//writeFile();//readFile();writeFile_2();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void readFile() throws Exception{RandomAccessFile raf = new RandomAccessFile("ran.txt", "r");//调整对象中指针,可以随意设置//raf.seek(8);//访问第二个对象了//跳过指定的字节数raf.skipBytes(8);//只能往后调,不能往回调byte[] buf = new byte[4];raf.read(buf);String name = new String(buf);int age = raf.readInt();System.out.println("name = " + name + ",age = " + age);raf.close();}public static void writeFile_2() throws Exception{RandomAccessFile raf = new RandomAccessFile("ran.txt", "rw");//raf.seek(8*3);//跳到指定位置raf.write("周期".getBytes());raf.writeInt(103);raf.close();}public static void writeFile() throws Exception{RandomAccessFile raf = new RandomAccessFile("ran.txt", "rw");raf.write("李四".getBytes());raf.writeInt(97);raf.write("王五".getBytes());raf.writeInt(98);raf.close();}
}

这篇关于Lesson_for_java_day18--java中的IO流(序列化、ByteArrayStream、DataStream、RandowAccessFile)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1126742

相关文章

Spring Security常见问题及解决方案

《SpringSecurity常见问题及解决方案》SpringSecurity是Spring生态的安全框架,提供认证、授权及攻击防护,支持JWT、OAuth2集成,适用于保护Spring应用,需配置... 目录Spring Security 简介Spring Security 核心概念1. ​Securit

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

SpringBoot改造MCP服务器的详细说明(StreamableHTTP 类型)

《SpringBoot改造MCP服务器的详细说明(StreamableHTTP类型)》本文介绍了SpringBoot如何实现MCPStreamableHTTP服务器,并且使用CherryStudio... 目录SpringBoot改造MCP服务器(StreamableHTTP)1 项目说明2 使用说明2.1

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Java中Arrays类和Collections类常用方法示例详解

《Java中Arrays类和Collections类常用方法示例详解》本文总结了Java中Arrays和Collections类的常用方法,涵盖数组填充、排序、搜索、复制、列表转换等操作,帮助开发者高... 目录Arrays.fill()相关用法Arrays.toString()Arrays.sort()A

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

Java堆转储文件之1.6G大文件处理完整指南

《Java堆转储文件之1.6G大文件处理完整指南》堆转储文件是优化、分析内存消耗的重要工具,:本文主要介绍Java堆转储文件之1.6G大文件处理的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言文件为什么这么大?如何处理这个文件?分析文件内容(推荐)删除文件(如果不需要)查看错误来源如何避