黑马程序员_java基础day21

------- android培训java培训、期待与您交流! ----------

主要内容:一、对象的序列化;二、管道流;三、RandomAccessFile;四、操作基本数据类型;五、操作字节数组;六、字符编码
一、对象的序列化:

ObjectInputStream与ObjectOutputStream
被操作的对象需要实现Serializable(标记接口);

静态成员不能被序列化。
transient:被修饰的成员不能序列化。


例:

[java]  view plain copy
  1. import java.io.*;  
  2. class  ObjectStreamDemo  
  3. {  
  4.     public static void main(String[] args) throws Exception  
  5.     {  
  6.         //writeObj();  
  7.         readObj();  
  8.     }  
  9.   
  10.     public static void readObj()throws Exception  
  11.     {  
  12.         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));  
  13.           
  14.         Person p = (Person)ois.readObject();  
  15.   
  16.         System.out.println(p);  
  17.         ois.close();  
  18.     }  
  19.   
  20.     public static void writeObj()throws IOException  
  21.     {  
  22.         ObjectOutputStream oos =   
  23.             new ObjectOutputStream(new FileOutputStream("obj.txt"));  
  24.   
  25.         oos.writeObject(new Person("lisi",30));  
  26.   
  27.         oos.close();  
  28.     }  
  29. }  

二、管道流:
PipedInputStream和PipedOutputStream
输入输出可以直接进行连接,通过结合线程使用。
例:

[java]  view plain copy
  1. import java.io.*;  
  2. class Read implements Runnable  
  3. {  
  4.     private PipedInputStream in;  
  5.     Read(PipedInputStream in)  
  6.     {  
  7.         this.in = in;  
  8.     }  
  9.     public void run()  
  10.     {  
  11.         try  
  12.         {  
  13.             byte[] buf = new byte[1024];  
  14.               
  15.             System.out.println("读取前。。没有数据阻塞");  
  16.             int len = in.read(buf);  
  17.             System.out.println("读到数据。。阻塞结束");  
  18.               
  19.             String s = new String (buf,0,len);  
  20.   
  21.             System.out.println(s);  
  22.   
  23.             in.close();  
  24.         }  
  25.         catch (IOException e)  
  26.         {  
  27.             throw new RuntimeException("管道读取流失败");  
  28.         }  
  29.     }  
  30. }  
  31. class Write implements Runnable  
  32. {  
  33.     private PipedOutputStream out;  
  34.     Write(PipedOutputStream out)  
  35.     {  
  36.         this.out = out;  
  37.     }  
  38.     public void run()  
  39.     {  
  40.         try  
  41.         {  
  42.             System.out.println("写入数据等待1秒");  
  43.             Thread.sleep(1000);  
  44.             out.write("piped lai la".getBytes());  
  45.             out.close();  
  46.         }  
  47.         catch (Exception e )  
  48.         {  
  49.             throw new RuntimeException("管道输出流失败");  
  50.         }  
  51.     }  
  52. }  
  53.   
  54. class PipedStreamDemo  
  55. {  
  56.     public static void main(String[] args) throws IOException  
  57.     {  
  58.         PipedInputStream in = new PipedInputStream();  
  59.         PipedOutputStream out = new PipedOutputStream();  
  60.         in.connect(out);  
  61.   
  62.         Read r = new Read(in);  
  63.         Write w = new Write(out);  
  64.   
  65.         new Thread(r).start();  
  66.         new Thread(w).start();  
  67.     }  
  68. }  

三、RandomAccessFile:
随机访问文件,自身具备读写的方法。
通过skipBytes(int x),seek(int x)来达到随机访问


RandomAccessFile


该类不算是IO体系中的子类。
而是直接继承自Object。


但是他是IO包中成员,因为他具备读和写功能。
内部封装了一个数组,而且通过指针对数组的元素进行操作。
可以通过getFilePointer获取指针位置,
同时可以通过seek改变指针的位置。


其实完成读写的原理就是内部封装了字节输入流和输出流。


通过构造函数可以看出,该类只能操作文件。
而且操作文件还有模式:只读r,读写rw等。


如果模式为只读 r。不会创建文件。会去读取一个已存在文件,如果该文件不存在,则会出现异常。
如果模式为rw。操作的文件不存在,会自动创建,如果存在则不会覆盖。
例:

[java]  view plain copy
  1. import java.io.*;  
  2. class  RandomAccessFileDemo  
  3. {  
  4.     public static void main(String[] args) throws IOException  
  5.     {  
  6.         writeFile();  
  7.         readFile();  
  8.           
  9.     }  
  10.     public static void readFile()throws IOException  
  11.     {  
  12.         RandomAccessFile raf = new RandomAccessFile("ran.txt","r");  
  13.           
  14.         //调整对象中指针。  
  15.         //raf.seek(8);  
  16.   
  17.         //跳过指定的字节数  
  18.         raf.skipBytes(8);  
  19.         byte[] buf = new byte[4];  
  20.   
  21.         raf.read(buf);  
  22.   
  23.         String name = new String(buf);  
  24.   
  25.         int age = raf.readInt();  
  26.   
  27.         System.out.println("name="+name);  
  28.         System.out.println("age="+age);  
  29.   
  30.         raf.close();  
  31.     }  
  32.     public static void writeFile()throws IOException  
  33.     {  
  34.         RandomAccessFile raf = new RandomAccessFile("ran.txt","rw");  
  35.   
  36.         raf.write("李四".getBytes());  
  37.         raf.writeInt(97);  
  38.         raf.write("王五".getBytes());  
  39.         raf.writeInt(99);  
  40.   
  41.         raf.close();  
  42.     }  
  43. }  
四、操作基本数据类型:
DataInputStream与DataOutputStream
可以用于操作基本数据类型的数据的流对象。
例:
[java]  view plain copy
  1. import java.io.*;  
  2. class DataStreamDemo   
  3. {  
  4.     public static void main(String[] args) throws IOException  
  5.     {  
  6.         //writeData();  
  7.         readData();  
  8.     }  
  9.     public static void readData() throws IOException  
  10.     {  
  11.         DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));  
  12.   
  13.         int num = dis.readInt();  
  14.         boolean b = dis.readBoolean();  
  15.         double d = dis.readDouble();  
  16.           
  17.         System.out.println("num="+num);  
  18.         System.out.println("b="+b);  
  19.         System.out.println("d="+d);  
  20.   
  21.         dis.close();  
  22.     }  
  23.     public static void writeData()throws IOException  
  24.     {  
  25.         DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));  
  26.   
  27.         dos.writeInt(567);  
  28.         dos.writeBoolean(false);  
  29.         dos.writeDouble(9886.897);  
  30.   
  31.         dos.close();  
  32.     }  
  33. }  

五、操作字节数组:
ByteArrayInputStream与ByteArrayOutputStream
用于操作字节数组的流对象。


ByteArrayInputStream:在构造的时候,需要接收数据源,而且数据源是一个字节数组。


ByteArrayOutputStream:在构造的时候,不用定义数据目的,因为该对象中已经内部封装了可变长度的字节数组。
这就是数据的目的地。


因为这两个流对象都操作的数组,并没有使用系统资源。
所以,不用进行close关闭。


在流操作规律讲解时:


源设备:
键盘 System.in,硬盘 FileStream,内存 ArrayStream。
目的设备:
控制台 System.out,硬盘FileStream,内存 ArrayStream。


用流的读写思想来操作数据。


操作字符数组:CharArrayReader与CharArraywrite
操作字符串:StringReader与SringWriter
例:

[java]  view plain copy
  1. import java.io.*;  
  2. class  ByteArrayStream  
  3. {  
  4.     public static void main(String[] args) throws IOException  
  5.     {  
  6.         //数据源。  
  7.         ByteArrayInputStream bis = new ByteArrayInputStream("ABCDE".getBytes());  
  8.   
  9.         //数据目的  
  10.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  11.           
  12.         int by = 0;  
  13.         while((by=bis.read())!=-1)  
  14.         {  
  15.             bos.write(by);  
  16.         }  
  17.         System.out.println(bos.size());  
  18.         System.out.println(bos.toString());  
  19.   
  20.         bos.writeTo(new FileOutputStream("a.txt"));  
  21.     }  
  22. }  

六、字符编码:
    1,字符流的出现为了方便操作字符。
更重要的是加入了编码转换。
通过子类转换流来完成
 InputStreamReader
 OutputStreamWriter
在两个对象进行构造的时候可以加入字符集。
例:

[java]  view plain copy
  1. import java.io.*;  
  2. class EncodeStream   
  3. {  
  4.     public static void main(String[] args) throws IOException  
  5.     {  
  6.         //writeText();  
  7.         readText();  
  8.     }  
  9.     public static void readText() throws IOException  
  10.     {  
  11.         InputStreamReader isr = new InputStreamReader(new FileInputStream("gbk.txt"),"GBK");  
  12.   
  13.         char[] buf = new char[10];  
  14.         int len = isr.read(buf);  
  15.   
  16.         String str = new String(buf,0,len);  
  17.         System.out.println(str);  
  18.     }  
  19.     public static void writeText()throws IOException  
  20.     {  
  21.         OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("utf.txt"),"utf-8");  
  22.   
  23.         osw.write("你好");  
  24.   
  25.         osw.close();  
  26.     }  
  27. }  

    2, 编码:字符串变成字节数组。


解码:字节数组变成字符串。


String-->byte[]; str.getBytes(charsetName);


byte[]-->String; new String(byte[],charsetName);
例:

[java]  view plain copy
  1. import java.util.*;  
  2. class  EncodeDemo  
  3. {  
  4.     public static void main(String[] args)throws Exception   
  5.     {  
  6.         String s = "你好";  
  7.   
  8.         byte[] b1 = s.getBytes("GBK");  
  9.   
  10.         System.out.println(Arrays.toString(b1));  
  11.         String s1 = new String(b1,"iso8859-1");  
  12.         System.out.println(s1);  
  13.   
  14.         //对s1进行iso8859-1编码  
  15.         byte[] b2 = s1.getBytes("iso8859-1");  
  16.         System.out.println(Arrays.toString(b2));  
  17.         String s2 = new String(b2,"gbk");  
  18.   
  19.         System.out.println(s2);  
  20.     }  
  21. }  

    3,字符编码——联通
    例:

[java]  view plain copy
  1. class EncodeDemo2   
  2. {  
  3.     public static void main(String[] args) throws Exception  
  4.     {  
  5.         String s = "联通";  
  6.   
  7.         byte[] by = s.getBytes("gbk");  
  8.   
  9.         for(byte b : by)  
  10.         {  
  11.             System.out.println(Integer.toBinaryString(b&255));  
  12.         }  
  13.     }  
  14. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值