首页 > 其他分享 >io

io

时间:2022-10-03 19:35:07浏览次数:41  
标签:fos import io FileInputStream new 序列化

流的分类

按方向:输入输出流

输入流:将<存储设备>中得内容读入到<内存>中,

输出流:将<内存>中得内容写入到<存储设备>中。

按单位:

字节流:以字节为单位,可以读写所有数据。

字符流:以字符为单位,只能读写文本数据。

按功能:

节点流:具有实际传输数据的读写功能。

过滤流:在节点流的基础上增强功能。

字节流

字节流有两个父类(抽象类)为流的读写提供基本操作。

InputStream 字节输入流   read

OutputStream 字节输出流  write

字节流抽象类的子类文件字节流

FileInputStream: read

FileOutputStream: write

package io;

import com.sun.org.apache.xml.internal.utils.res.StringArrayWrapper;

import java.io.FileInputStream;

/*
* FileInputStream
*
* */
public class Demo01 {
public static void main(String[] args) throws Exception {
//创建FileInputStream,并指定文件路径
FileInputStream fis=new FileInputStream("d:\\AAA.txt");
//一个个读取文件
/*int data=0;
while((data=fis.read())!=-1){
System.out.print((char)data);
}*/

//一次读取多个
byte[] buf=new byte[3];
int count=0;
while ((count=fis.read(buf))!=-1){
System.out.println(new String(buf,0,count));
}
//关闭
fis.close();
System.out.println("执行完毕");
}
}

 ackage io;

import java.io.FileOutputStream;


public class Demo02 {
public static void main(String[] args) throws Exception{
//创建一个FileOutStream对象
FileOutputStream fos=new FileOutputStream("d:\\BBB.txt",true);
//写入文件
fos.write(97);
fos.write('b');
fos.write(99);
fos.write('d');
String string="helloWorld";
fos.write(string.getBytes());

//关闭
fos.close();
System.out.println("执行完毕");
}

利于字节流复制文件

package io;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Demo03 {
public static void main(String[] args) throws Exception{
//创建流
//文件输入流
FileInputStream fis=new FileInputStream("d:\\BBB.txt");
//文件输出流
FileOutputStream fos=new FileOutputStream("d:\\CCC.txt");
//一边读一边写
byte[] buf=new byte[1024];
int count=0;
while((count=fis.read(buf))!=-1){
fos.write(buf,0,count);
}
fis.close();
fos.close();
System.out.println("复制完毕");
}

}
字节缓冲流
: BufferedInputStream/BufferedOutputStream
提高io效率,减少访问磁盘的次数;
数据存储在缓冲区中,flush是缓冲区的内容写入文件中,也可以直接close。
package io;

import java.io.BufferedInputStream;
import java.io.FileInputStream;

public class Demo04 {
public static void main(String[] args) throws Exception{
//创建缓冲字节输入流
FileInputStream fis=new FileInputStream("d:\\BBB.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
//读取
int data=0;
while((data=bis.read())!=-1){
System.out.println((char)data);
}
//关闭
bis.close();
System.out.println("读取完毕");
}
}
package io;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;


public class Demo05 {
public static void main(String[] args) throws Exception{
//创建缓冲字节输出流
FileOutputStream fos=new FileOutputStream("Buffer.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);
//写入文件
for (int i = 0; i < 10; i++) {
bos.write("cnmd".getBytes());//写入缓冲区
bos.flush();//刷新到硬盘
}
bos.close();
System.out.println("写出成功");
}


}
对象流
ObjectIntputStream/ObjectOutputStream
增强了缓冲区功能
增强了的种基本数据类型和字符串功能
增强了读写对象的功能
向流中写入一个对象被称为序列化
在流中读取一个对象被称为反序列化
序列化类必须要实现Serializable接口
序列化中的对象属性也需要实现Serializable接口
使用transient(瞬间的)修饰的属性,该属性不能序列化。
静态属性也不能序列化。
在序列化类中添加序列化版本ID,以保证序列化和反序列化后的类是同一种类型
可以借助集合来序列化多个对象
private static final long serialVersionUID


package io;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

//
public class Demo06 {
public static void main(String[] args) throws Exception{
//序列化
FileOutputStream fos=new FileOutputStream("d:\\stu.bin");
ObjectOutputStream oos=new ObjectOutputStream(fos);
//序列化(写入对象)
//创建对象
Student s1=new Student("张三",24);
Student s2=new Student("李四",24);
ArrayList<Student> list=new ArrayList<Student>();
list.add(s1);
list.add(s2);
oos.writeObject(list);
//关闭
oos.close();
System.out.println("序列化完成");
}
}
package io;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;

public class Demo07 {
public static void main(String[] args) throws Exception{
FileInputStream fis=new FileInputStream("d:\\stu.bin");
ObjectInputStream ois=new ObjectInputStream(fis);
//反序列化
// Student stu1=(Student) ois.readObject();
//Student stu2=(Student) ois.readObject();
ArrayList<Student> list=(ArrayList<Student>)ois.readObject();
//关闭
ois.close();
System.out.println("反序列化完成");
System.out.println(list.toString());
}
}
字符流


字符流的父类(抽象类)
Reader/Writer

标签:fos,import,io,FileInputStream,new,序列化
From: https://www.cnblogs.com/zlsame/p/16732063.html

相关文章

  • SpringBoot访问Clickhouse执行时报错:org.springframework.beans.factory.UnsatisfiedD
    1依赖信息<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"......
  • SAS - Data Set Options and Statement
    SASdataset有很多针对数据集的选项(option),这些选项都有同名的statement.常见的有:OptionsStatementKEEP=KEEPDROP=DROPRENAME=REANMELABEL=LABEL......
  • Visual Studio Code= 笔记
    第一次ES6//letschool='magedu'//console.log(school.charAt(2))//g//console.log(school[2])//g//console.log(school.toUpperCase())//MAGEDU//console......
  • commons-io使用
    引入依赖<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependency>案例1importorg.apache......
  • commons-collections4使用
    引入依赖<dependency><groupId>org.apache.commons</groupId><artifactId>commons-collections4</artifactId><version>4.4</version></dependency>案例1......
  • net.schmizz.sshj.transport.TransportException:
    主要是在使用jprofiler同时修改了server的端口之后出现的问题,记录下解决临时方法(毕竟jprofiler代码混淆了)参考解决方法核心是使用ssh-keyscan-ted25519生成指纹信息参......
  • Visual Studio批量删除所有注释
    ——上方工具栏—搜索—在文件中替换(或者Command+Shift+H)——勾选上​​正则表达式​​搜索,查找//.*\n,替换为空即可......
  • sessionStorage与localStorage 设置与获取
        <!DOCTYPEhtml><html><head><title>本地存储</title><script>functionsetSessionStorage(){//添加sessionStoragevarname......
  • Contrastive Learning for Cold-Start Recommendation阅读笔记
    动机本文是2021年ACMMM上的一篇论文。之前关于推荐系统冷启动的工作很多都使用神经网络来探索冷物品的特征内容和协同表示之间的联合效应,但是作者认为这些工作很少探索内......
  • 【THM】Metasploit: Introduction(Metasploit简介)-学习
    介绍Metasploit是应用最广泛的利用框架之一,它是一个强大的工具,可以支持渗透测试的所有阶段,从信息收集到后渗透。Metasploit有两个主要版本:MetasploitPro:促进任......