day22 Java学习 IO流(序列流)

IO流(序列流)

     序列流:

                  * 可以把多个字节输入流整合成一个,从序列流中读取数据时,将从被整合的第一个流开始读,读完一个之后继续读第二个。

    整合方式:

                  * Seq uenceInputStream ( InputStream ,InputStream )

 

IO流(序列流整合多个)

    public static void main(String[] args) throws IOException {FileInputStream fis1 = new FileInputStream("test1.txt");FileInputStream fis2 = new FileInputStream("test2.txt");FileInputStream fis3 = new FileInputStream("test.txt");Vector<InputStream> v = new Vector<>();v.add(fis1);v.add(fis2);v.add(fis3);Enumeration<InputStream> en = v.elements();SequenceInputStream sis = new SequenceInputStream(en);FileOutputStream fos = new FileOutputStream("list.txt");int b;while ((b = sis.read()) != -1) {fos.write(b);}sis.close();fos.close();}
例子

 

IO流(内存输出流)

  内存输出流:

               * 该输出可以向内存中写数据,把内存当作一个缓冲区,写出之后可以一次性获取出所有数据。

    public static void main(String[] args) throws IOException {FileInputStream fis1 = new FileInputStream("test1.txt");ByteArrayOutputStream by = new ByteArrayOutputStream(); // 在内存中创建了可以增长的内存数组int b;while ((b = fis1.read()) != -1) { // 将读到的数据逐个写到内存中
            by.write(b);}System.out.println(by); // 将缓冲区的内容转换成了字符串
        fis1.close();}
例子

 

IO流(随机访问流和读写数据)

   随机访问流:

                      * RandomAccessFile类不属于流,是Object类的子类,但他融合了InputStream和OutputStream的功能。支持对随机访问文件的读取和写入。

    seek():设置指定位置读和写

 

IO流(对象操作流 )

   对象操作流 :该流可以将一个对象写出。或者读取一个对象到程序中,也就是执行了序列化和反序列化的操作。       

   使用方式:

               new ObjectOutputStream(OutputStream),writeObject( )。 

               new ObjectInputStream(OutputStream)    对象输入流(反序列化)

 

IO流(对象操作流优化)

     

    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {Person p1 = new Person("张三", 14);Person p2 = new Person("李四", 15);Person p3 = new Person("王五", 16);ArrayList<Person> list = new ArrayList<>(); list.add(p1);list.add(p2);list.add(p3);ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e.txt"));oos.writeObject(list);//将整个集合一次写出
        oos.close();ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt"));ArrayList<Person> list1 = (ArrayList<Person>) ois.readObject();  //将整个集合一次读取for (Person person : list1) {System.out.println(person);}ois.close();}
优化例子

 

IO流(数据输入输出流)

    数据输入输出流:

              * DataInputStream,DataOutputStream可以按照基本数据类型大小读写数据。

    使用方式:

    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {DataOutputStream dos=new DataOutputStream(new FileOutputStream("g.txt"));dos.writeInt(997);dos.writeInt(998);dos.writeInt(999);dos.close();DataInputStream dis=new DataInputStream(new FileInputStream("g.txt"));int a=dis.readInt();int b=dis.readInt();int c=dis.readInt();System.out.println(a);System.out.println(b);System.out.println(c);}
例子

 

 IO流(Properties概述和作为Map集合的使用)

      Properties的概述:

                     * Properties是Hashtable的子类。

                     * Properties类表示了一个持久的属性集。

                     * Properties可保存在流中或从流中加载。

                     * 属性列表中每个键及其对象值都是一个字符串

    public static void main(String[] args) {Properties p= new Properties();p.put("a", 1);System.out.println(p);System.out.println(p.get("a"));}
例子

 

  IO流(Properties的特殊功能使用)

      * public Object setProperty( String key ,Sring value) :设置键和值

      * public String  getProperty( String key) :根据键来获取值

      * public Enumeration <string>   :迭代    stringPropertyNames():拿到所有的键 

    public static void main(String[] args) {Properties p= new Properties();p.setProperty("a", "2");p.setProperty("b", "3");Enumeration<String> en= (Enumeration<String>) p.propertyNames();while (en.hasMoreElements()) {String key=en.nextElement();String value=p.getProperty(key);System.out.println(key+"="+value);}}
例子

 

IO流(Properties的load( )和Store ( ))

   

    public static void main(String[] args) throws FileNotFoundException, IOException {Properties p= new Properties();System.out.println("读取前:"+p);p.load(new FileInputStream("a.txt"));      //load()读取文件System.out.println("读取后:"+p);p.setProperty("name", "lisi");p.store(new FileOutputStream("a.txt"), null); //store()第二个参数是用来描述文件列表的,如果不描述可以传NULLSystem.out.println("添加后:"+p);}
例子

 

转载于:https://www.cnblogs.com/feng0001/p/10965452.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/277584.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

网站建设-简单动态网站搭建

通过前面Clouder课程的学习&#xff0c;或许你已经掌握了在云服务器上发布和部署静态网页的方法&#xff0c;那么如何搭建一个可以随时更新内容的动态网站&#xff1f;通过本课程的学习&#xff0c;你将掌握如何在云端搭建全世界使用最多的WordPress网站的方法&#xff0c;并学…

mysql的concat函数_MySQL中concat函数(连接字符串)

MySQL中concat函数使用方法&#xff1a;CONCAT(str1,str2,…)返回结果为连接参数产生的字符串。如有任何一个参数为NULL &#xff0c;则返回值为 NULL。注意&#xff1a;如果所有参数均为非二进制字符串&#xff0c;则结果为非二进制字符串。如果自变量中含有任一二进制字符串&…

利用airTest的图像实别技术测试Web应用

airTest的第三方类库中有图像实别功能&#xff0c;根据官网的介绍&#xff0c;这个功能是能够在Windows上用来定位元素&#xff0c;进行操作的。尝试过以下脚本&#xff0c;发现真的可以。 from selenium.webdriver.chrome.options import Options from selenium import webdri…

MySQL主从复制故障解决

丛库复制停止&#xff0c;进丛库查看&#xff0c;报错1007&#xff0c;数据库已存在&#xff0c;不能创建数据库 mysql> show slave status\G; Slave_IO_Running: Yes Slave_SQL_Running: No Last_Errno: 1007 Last_Error: Error Cant create database test; database exis…

Unraveling the JPEG file

(文章还剩实践部分没写&#xff0c;答辩过后补上...) JPEG文件在当下数字化生活中是无处不在的&#xff0c;但是在熟悉的JPEG面纱背后&#xff0c;隐藏着一些算法&#xff0c;它们去除了人类眼中无法察觉到的细节。这产生了最高的视觉质量与最小的文件大小。让我们来看看这一算…

mysql interval 3 day_Mysql之INTERVAL与DATE_SUB与EXTRACT函数的使用

1. INTERVALINTERVAL代表的是时间间隔MySQL中的时间间隔类型有如下几种:1.1 利用INTERVAL做时间的加减法示例&#xff1a;加法:SQL>SELECT DATE 2018-11-01 INTERVAL 10 11 DAY_HOUR;结果:2018-11-11 11:00:00减法&#xff1a;SQL> select date 2018-11-11 11:00:00 -INT…

(二十四)面向对象

class Car {int num;String name;String color;public static void run() {System.out.println("行驶中");} } //再类中定义的变量&#xff1a;成员变量 //在类中定义的函数&#xff1a;成员函数 class Demo1 {public static void main(String[] args) {//创建一个ca…

mysql 三主_MySQL主主复制3

一、创建并授权用户在每一台(主)服务器上创建一个用户&#xff0c;并为之授权&#xff0c;使它们可以互相访问彼此的数据库在Server-1上&#xff1a;创建一个充许Server-2来访问的用户server2,密码为&#xff1a;server2mysql> GRANT REPLICATION SLAVE ON *.*> TO ‘ser…

0727日志

为什么80%的码农都做不了架构师&#xff1f;>>> c端线上地址 http://x.diandanme.com/fe/?d183#/ 什么时候来需求&#xff0c;我做好准备了吗&#xff1f; eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC94LmRpYW5kYW5tZS5jb21cL2FwaVwvd2VjaGF0XC…

Python基础学习总结__Day3

一、集合 1&#xff0e;特性&#xff1a;无序且天生去重&#xff0c;格式为{} 2&#xff0e;作用&#xff1a; &#xff08;1&#xff09;去重 &#xff08;2&#xff09;关系测试 3&#xff0e;可调用函数&#xff08;常见对列表操作&#xff09; &#xff08;1&#xff09;取…

day8网络编程,面向对象1

一.只是回顾 1.导入模块的顺序,首先从当前目录下找,再从环境变量里面找,使用"sys.path.insert(0,需要导入的环境变量)"加入需要导入文件的环境变量; 2.如果不同项目中有相同的文件,需要导入文件,需要将非当前项目中右键添加的环境变量取消,将当前的环境变量添加上去;…

mysql练手数据_MySQL新手练习

操作插入数据 的语法 :INSERT INTO 表名称 VALUES(值1&#xff0c;值2&#xff0c;......);修改数据 的语法&#xff1a;UPDATE 表名称 SET 字段名1 值1, 字段名2值2,...... 【WHERE 条件】;删除数据 的语法&#xff1a;delete from 表名 【[where 条件】;delete 表1&#xff…

spring security config

spring secuirty 相关的配置解析 permitAll()与web.ignoring() ingore是完全绕过了spring security的所有filter&#xff0c;相当于不走spring securitypermitall没有绕过spring security&#xff0c;其中包含了登录的以及匿名的。转载于:https://www.cnblogs.com/MND1024/p/10…

TSPITR方式数据库找回误操作丢失的数据

一、TSPITR介绍 TSPITR全称是Tablespace Point In Time Recover&#xff08;表空间基于时间点的不完全恢复&#xff09;。原理是通过辅助实例基于时间还原出误操作前的数据通过DataPump将数据导入到目标数据库。TSPITR的最大好处是不需要生产库停机。 二、适用场景 表空间时点恢…

乌班图系统的MySQL_乌班图系统mysql主从备份

一&#xff0e;准备系统&#xff1a;ubuntu 14.04.2 LTSMysql: server version 5.5.43两台主机可以互相通信&#xff1a;192.168.1.11 master192.168.1.12 slave二&#xff0e;步骤Master部分&#xff1a;1.创建备份帐号&#xff1a;slave密码&#xff1a;slaveGrant repl…

mysql table alter_MySQL-ALTER TABLE命令学习[20180503]

学习ALTER TABLE删除、添加和修改字段和类型CREATE TABLE alter_tab01(id int,col01 char(20))enginInnoDB default charsetutf8;删除字段ALTER TABLE DROP ;mysql> alter table alter_tab01 dropcol01;Query OK, 0 rows affected (0.01sec)Records: 0 Duplicates: 0 Warnin…

时间戳转换

13 位时间戳转换 1 通过java&#xff0c;如下&#xff1a; public static String timeStamp2Date(String time) {Long timeLong Long.parseLong(time);SimpleDateFormat sdf new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//要转换的时间格式Date date;try {date …

React.Component(V16.8.6)

组件的生命周期 挂载 当组件实例被创建并插入 DOM 中时&#xff0c;其生命周期调用顺序如下&#xff1a; constructor()static getDerivedStateFromProps()render()componentDidMount()componentWillMount() 之后将废弃 更新 当组件的 props 或 state 发生变化时会触发更新。组…

mysql date类型加一个月jdbc_JDBC操作数据库Date类型数据

JDBC操作数据库Date类型数据由于java原生的工具类java.util提供的Date对象与JDBC提供的Date对象并不相同分别是java.util.Date和java.sql.Datejava.sql.Date是java.util.Date的子类所以在进行增删改查部分操作中&#xff0c;不能直接将原生工具类的Date对象直接运用到JDBC中可以…

使用深度学习TensorFlow框架进行图片识别

Apsara Clouder大数据专项技能认证&#xff1a;使用深度学习TensorFlow框架进行图片识别本认证系统的介绍了深度学习的一些基础知识&#xff0c;以及Tensorflow的工作原理。通过阿里云机器学习PAI基于经典的CIFAR-10数据集实现图片识别。学员可以通过本实验&#xff0c;对深度学…