Java上路19-I/O-常用类


一. 对象序列化:

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());
       }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值