第21天-01-IO流(对象的序列化)

ObjectInputStream与ObjectOutputStream

  • 被操作的对象需要实现Serializable接口(标记接口)
  • 非必须, 但强烈建议所有可序列化类都显式声明serialVersionUID
package bxd;

import java.io.*;

public class ObjectStreamDemo {
public static void readObj() throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Person.object"));
Person person = (Person) ois.readObject();
System.out.println(person);
ois.close();
} public static void writeObj() throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Person.object"));
oos.writeObject(new Person("lily", 39, "us"));
oos.close();
} public static void main(String[] args) throws Exception {
// writeObj();
readObj();
}
} /*
输出lily:0:cn, 因为age不会被序列化(使用初始值0), 静态变量country也不会被序列化(使用初始值cn).
*/
class Person implements Serializable { public static final long serialVersionUID = 42L; // 强烈建议所有可序列化类都显式声明serialVersionUID
private String name;
transient int age; // 如果某个实例变量不需要被序列化, 可以使用transient修饰
static String country = "cn"; // 序列化行为只针对Java堆(heap), 而静态变量不存在于heap. Person(String name, int age, String country) {
this.name = name;
this.age = age;
this.country = country;
} public String toString() {
return name + ":" + age + ":" + country;
}
}

第21天-02-IO流(管道流)

PipedInputStream和PipedOutputStream

  • 输入和输出可以直接进行连接, 通常结合线程使用.
  • 不建议对这两个对象尝试使用单个线程, 因为这样可能发生线程死锁
package bxd;

import java.io.*;

public class PipedStreamDemo {

    public static void main(String[] args) throws IOException {

        PipedInputStream pipedInputStream = new PipedInputStream();
PipedOutputStream pipedOutputStream = new PipedOutputStream();
// 只要往pipedOutputStream写入的内容, 就可以从pipedInputStream读到
pipedInputStream.connect(pipedOutputStream); Read read = new Read(pipedInputStream);
Write write = new Write(pipedOutputStream); new Thread(read).start();
new Thread(write).start();
}
} class Write implements Runnable { private PipedOutputStream out; public Write(PipedOutputStream out) {
this.out = out;
} @Override
public void run() {
try {
System.out.println("开始执行PipedOutputStream操作: ");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); String line = null;
while ((line = bufferedReader.readLine()) != null) {
if ("over".equals(line)) break;
out.write(line.getBytes());
out.write(System.lineSeparator().getBytes());
} bufferedReader.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} class Read implements Runnable { private PipedInputStream in; public Read(PipedInputStream in) {
this.in = in;
} @Override
public void run() {
try {
System.out.println("开始执行PipedInputStream操作: "); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("读取到的内容.txt")); byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) != -1) {
bufferedOutputStream.write(buf, 0, len);
} bufferedOutputStream.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

毕向东_Java基础视频教程第21天_IO流(1)的更多相关文章

  1. 毕向东_Java基础视频教程第20天_IO流(7~10)

    第20天-07-IO流(递归) package bxd; import java.io.File; public class FileDemo3 { // 非递归打印 public static vo ...

  2. 毕向东_Java基础视频教程第19天_IO流(01~05)

    第19天-01-IO流(BufferedWriter) 字符流的缓冲区 缓冲区的出现提高了对数据的读写效率. 对应类缓冲区要结合流才可以使用. BufferedWriter BufferedReade ...

  3. 毕向东_Java基础视频教程第19天_IO流(06~10)

    第19天-06-IO流(装饰设计模式) 装饰设计模式: 当想要对已有的对象进行功能增强时, 可以定义类,将已有对象传入,基于已有的功能,并提供加强功能.那么这个自定义的类称为装饰类. 装饰类通常会通过 ...

  4. 毕向东_Java基础视频教程第19天_IO流(11~14)

    第19天-11-IO流(字节流File读写操作) import java.io.FileInputStream; import java.io.FileOutputStream; import jav ...

  5. 毕向东_Java基础视频教程第20天_IO流(15~17)

    第20天-15-IO流(打印输出流) 打印输出流:PrintWriter与PrintStream 两者的区别:Since JDK 1.4 it's possible to specify the ch ...

  6. 毕向东_Java基础视频教程第20天_IO流(11~14)

    第20天-11-IO流(Properties简述) .properties是一种主要在Java相关技术中用来存储应用程序的可配置参数的文件的文件扩展名.它们也可以存储用于国际化和本地化的字符串,这种文 ...

  7. 毕向东_Java基础视频教程第20天_IO流(5~6)

    第20天-05-IO流(文件列表一) static File[] listRoots() List the available filesystem roots. String[] list() Re ...

  8. 毕向东_Java基础视频教程第20天_IO流(1~4)

    第20天-01-IO流(File概述) File类: 用来将文件或者文件夹封装成对象, 方便进行操作. File对象可以作为参数, 传递给流对象的构造函数. 流对象不能操作文件夹; 流对象不能操作文件 ...

  9. 毕向东_Java基础视频教程第19天_IO流(20~22)

    第19天-20-IO流(改变标准输入输出设备) static void setIn(InputStream in) Reassigns the "standard" input s ...

随机推荐

  1. Android Studio: /dev/kvm device permission denied

    https://stackoverflow.com/questions/37300811/android-studio-dev-kvm-device-permission-denied To chec ...

  2. 解决: selenium webdriver unable to find Mozilla geckodriver

    1 安装 sudo apt-get install libqt4-dev libqtwebkit-dev 2 gem install capybara-webkit 3 在rails-helper.r ...

  3. IDEA 启动项目,tomcat中配置的虚拟路径无法使用

    有时候,使用idea启动项目,非动静分离项目,直接根据图片url地址显示图片,会发现图片无法显示,tomcat中配置的虚拟路径无法使用,这时候需要配置idea.选择路径,然后给与一个访问名就行了.

  4. linux 安装jdk 二进制版本,非安装版

    0.下载jdk8 登录网址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html选择对 ...

  5. Intellij Idea快捷鍵

    一.视图查看 Ctrl+F12 查看file,method结构图.类继承机构图 (不知道方法结构,Ctrl+F12一下,方法,参数,返回值,一清二楚的展现出来) Ctrl+shift+Alt+U   ...

  6. Generic-Host 快速使用指南

    .NETCORE 中的 Generic Host 本文以自己在工作中学习和使用.net core generic-host 作一个总结. 前言 在创建的ASPNETCORE项目中,我们可以在Main( ...

  7. A/B_test改变新旧网页 观察用户的引流效果

    代码处:https://github.com/xubin97/Data-analysis_exp2 分析A/B测试结果 目录 简介 I - 概率 II - A/B 测试 简介 首先这个项目数据来自某公 ...

  8. windows 10安装gensim、nltk

    一.安装gensim 1.什么事gensim gensim是一个python的科学库,gensim包含了TF-IDF.随机投影.word2vec和document2vec算法的实现,分层Dirchle ...

  9. json和xml以及ajax的数据格式用法

    JSON的两个方法: 1.将字符串转换为JSON格式:parse(). 2.将原生JavaScript值转换为JSON字符串:stringify(); <!DOCTYPE html> &l ...

  10. XAMPP环境的搭建

    XAMPP是一个强大的集成软件包(什么是集成软件包?就是多个软件打包一起安装了,比如office办公软件包括了word.Excel.PPT) XAMPP包括了Apache,MySQL,PHP,Perl ...