Java上路19-I/O-常用类
来源:互联网 发布:唐嫣紫霞仙子 知乎 编辑:程序博客网 时间:2024/06/16 18:08
一. 对象序列化:
Java对象序列化将那些实现了Serializable接口的对象转换成一个字节序列,并能够以后将这个字节序列完全恢复为原来的对象。利用对象的序列化,可以实现轻量级持久性,这意味着一个对象的生存周期并不取决于程序是否正在执行,它可以生存于程序的调用之间。通过将一个序列化对象写入磁盘,然后在重新调用程序时恢复该对象,就能够实现持久性的效果。ObjectOutputStream / ObjectInputStream 。
测试类:
import java.io.*; //类通过实现Serializable接口以启用其序列化功能class Test implements Serializable{ private String name; private boolean used; static int age; //静态的不可被序列化 transient intsex; //声明为不可序列化的成员 public static final long serialVersionUID = 43L; //自定义的UID Test(String name, boolean used, int age, int sex) { this.name=name; this.used=used; this.age=age; this.sex=sex; } public StringtoString() { return "Name : "+name+", usefull : "+used+", age :"+age+", sex : "+sex; }}
操作类:
import java.io.*; class FileDemo{ public static void main(String[] args) throws Exception { //writeObj(); readObj(); } public static void writeObj() throws Exception { ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("obj.txt")); //将对象序列化后的数据保存在txt文件 oos.writeObject(newTest("Test", true, 24, 1)); //写入目标对象 oos.close(); } public static void readObj() throws Exception { ObjectInputStream ois=new ObjectInputStream(new FileInputStream("obj.txt")); //读取现有的数据反序列化 Test test=(Test)ois.readObject(); //实例化序列中的对象 System.out.println(test); ois.close(); }}
首先编译Test类,然后开启操作类的writeObj()方法注释readObj()方法编译执行,再注释writeObj()方法开启readObj()方法编译执行。如果已经有了一个序列化的文件,可以只有读取的方法。
二. 管道流:
管道,它具有将一个程序的输出当作另一个程序的输入的能力。在Java中,可以使用管道流进行线程之间的通信,输入流和输出流必须相连接,不需要共享的数据空间。涉及多线程。
字节流:PipedOutputStream和PipedInputStream
字符流:PipedWriter和PipedReader
import java.io.*; class Read implements Runnable{ private PipedInputStream in; //管道读取 Read(PipedInputStream in) { this.in=in; } public voidrun() { try { byte[]buf=new byte[1024]; int len=(in.read(buf)); String s=new String(buf, 0, len); System.out.println(s); in.close(); } catch(IOException e) { thrownew RuntimeException("管道流读取失败"); } }} class Write implements Runnable{ private PipedOutputStream out; //管道写入 Write(PipedOutputStream out) { this.out=out; } public voidrun() { try { out.write("管道接通".getBytes()); } catch(IOException e) { thrownew RuntimeException("管道流写入失败"); } }} class PipelineDemo{ public static void main(String[] args) throws IOException { PipedInputStream in=new PipedInputStream(); PipedOutputStream out=new PipedOutputStream(); in.connect(out); Read r=new Read(in); Write w=new Write(out); newThread(r).start(); newThread(w).start(); }}
三. RandomAccessFile:
此类的实例支持对随机访问文件的读取和写入。如果模式为r,不会创建文件,而是读取已存在的文件;如果不存在则发生异常。如果模式为rw,当文件不存在时自动创建;如果存在,将直接读取修改。
import java.io.*; class RafDemo{ public static void main(String[] args) throws IOException { //writeFile(); readFile(); } public static void writeFile() throws IOException { RandomAccessFile raf=new RandomAccessFile("obj.txt", "rw"); raf.write("hello".getBytes()); raf.writeInt(48); raf.write("wod".getBytes()); raf.writeInt(48); raf.close(); } public static void readFile() throws IOException { RandomAccessFile raf=new RandomAccessFile("obj.txt", "r"); //raf.seek(8); //从第8位开始读取 raf.skipBytes(8); //跳过前8位 byte[]buf=new byte[5]; //一次读取5个字节 raf.read(buf); String name=new String(buf); System.out.println("name"+name); raf.close(); }}
四. DataStream:
可以用于操作基本数据类型的数据的流对象。
import java.io.*; class RafDemo{ public static void main(String[] args) throws IOException { //write(); read(); } public static void write() throws IOException { DataOutputStream dos=new DataOutputStream(new FileOutputStream("obj.txt")); dos.writeInt(520); dos.writeBoolean(true); dos.writeDouble(2340.2340); dos.writeUTF("Java上路系列"); dos.close(); } public static void read() throws IOException { DataInputStream dis=new DataInputStream(new FileInputStream("obj.txt")); int i=dis.readInt(); boolean b=dis.readBoolean(); double d=dis.readDouble(); String s=dis.readUTF(); dis.close(); System.out.println(i+","+b+", "+d+", "+s); }}
五. 字节数组:
用于操作字节数组的流对象
import java.io.*; class RafDemo{ public static void main(String[] args) throws IOException { //s数据源 ByteArrayInputStream bis=new ByteArrayInputStream("66666ABCDEF".getBytes()); //数据目的 ByteArrayOutputStream bos=new ByteArrayOutputStream(); intby=0; while((by=bis.read())!=-1) { bos.write(by); } System.out.println(bos.size()); System.out.println(bos.toString()); }}
- Java上路19-I/O-常用类
- Java上路16-I/O
- Java上路18-I/O-File
- Java I/O流常用类小结
- Java上路17-I/O-字符与字节
- Java I/O类
- java的常用I/O流~~
- Java I/O常用流示例
- java I/O之常用操作
- java I/O流常用代码
- 常用java中I/O流
- java 【I/O】 类 结构图
- java常用类解析四:I/O流典型使用方式
- java常用类解析四:I/O流典型使用方式
- Java-I/O操作之字符流、其他常用流
- Java I/O系统----------- 标准I/O
- Socket Java I/O Socket I/O
- java i/o 类之File
- Ubuntu10.04制作官方源镜像以及搭建本地
- LSI设计(2)布局算法(待上载)
- 3天学习haskell---haskell第二天
- c/c++中extern用来声明全局变量
- LSI设计(3)布线算法(待上载)
- Java上路19-I/O-常用类
- LSI设计(4)FOCR快速最优通道布线
- 博弈进行时~
- Visual Studio 2005各试用版官方下载地址 .
- LSI设计(5)崎岖通道最优布线
- 讲解MySQL索引的概念及数据库索引的应用
- SQL server 容易让人误解的问题之 聚集表的物理顺序问题
- ARCGIS viewer入门(3) Widget共享数据
- PKCS#7格式数字签名验证 .