Java基础--IO流之其他流

凌风博客原创作品。转载请注明出处:http://blog.csdn.net/q549130180/article/details/45333479


打印流:

该流提供了打印方法,可以将各种数据类型的数据都原样打印


字节打印流:
PrintStream
构造函数可以接收的参数类型:
1.file对象,    File
2.字符串路径。  String
3.字节输出流。  OutputStream


字符打印流
PrintWriter
构造函数可以接收的参数类型:
1.file对象,    File
2.字符串路径。  String
3.字节输出流。  OutputStream

4.字符输出流。  Writer

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.io.*;  
  2. class PrintStreamDemo_1  
  3. {  
  4.     public static void main(String[] args) throws IOException  
  5.     {  
  6.         BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));  
  7.   
  8.         PrintWriter out = new PrintWriter(new FileWriter("a.txt"),true);  
  9.   
  10.         String line = null;  
  11.   
  12.         while ((line=bufr.readLine())!=null)  
  13.         {  
  14.             if("over".equals(line))  
  15.                 break;  
  16.             out.println(line.toUpperCase());  
  17.             out.flush();  
  18.         }  
  19.   
  20.         out.close();  
  21.         bufr.close();  
  22.     }  
  23. }  

合并流

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.io.*;  
  2. import java.util.*;  
  3. class SequenceDemo_2   
  4. {  
  5.     public static void main(String[] args) throws IOException  
  6.     {  
  7.         Vector<FileInputStream> v = new Vector<FileInputStream>();  
  8.   
  9.         v.add(new FileInputStream("c:\\1.txt"));  
  10.         v.add(new FileInputStream("c:\\2.txt"));  
  11.         v.add(new FileInputStream("c:\\3.txt"));  
  12.   
  13.         Enumeration<FileInputStream> en = v.elements();  
  14.           
  15.         //合并流对象  
  16.         SequenceInputStream sis = new SequenceInputStream(en);  
  17.   
  18.         FileOutputStream fos = new FileOutputStream("c:\\4.txt");  
  19.   
  20.         byte[] buf = new byte[1024];  
  21.   
  22.         int len = 0;  
  23.   
  24.         while ((len=sis.read(buf))!=-1)  
  25.         {  
  26.             fos.write(buf,0,len);  
  27.         }  
  28.         fos.close();  
  29.         sis.close();  
  30.     }  
  31. }  

切割文件

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.io.*;  
  2. import java.util.*;  
  3. class SplitFile_3   
  4. {  
  5.     public static void main(String[] args) throws IOException  
  6.     {  
  7.         //splitFile();  
  8.         merge();  
  9.     }  
  10.       
  11.     //合并文件  
  12.     public static void merge() throws IOException  
  13.     {  
  14.         ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();  
  15.   
  16.         for (int x =1;x<=3 ;x++ )  
  17.         {  
  18.             al.add(new FileInputStream("D:\\abc\\a\\"+x+".part"));  
  19.         }  
  20.   
  21.         //ArrayList无法创建Enumeration对象,使用Iterator来创建  
  22.         final Iterator<FileInputStream> it = al.iterator();  
  23.           
  24.         //创建Enumeration对象  
  25.         Enumeration<FileInputStream> en = new Enumeration<FileInputStream>()  
  26.         {  
  27.             public boolean hasMoreElements()  
  28.             {  
  29.                 return it.hasNext();  
  30.             }  
  31.             public FileInputStream nextElement()  
  32.             {  
  33.                 return it.next();  
  34.             }  
  35.         };  
  36.           
  37.         //创建合并流对象  
  38.         SequenceInputStream sis = new SequenceInputStream(en);  
  39.           
  40.         //输出文件位置  
  41.         FileOutputStream fos = new FileOutputStream("D:\\abc\\a\\0.mp3");  
  42.   
  43.         byte[] buf = new byte[1024];  
  44.   
  45.         int len = 0;  
  46.   
  47.         while ((len=sis.read(buf))!=-1)  
  48.         {  
  49.             fos.write(buf,0,len);  
  50.         }  
  51.   
  52.         fos.close();  
  53.         sis.close();  
  54.     }  
  55.       
  56.   
  57.     //切割文件  
  58.     public static void splitFile() throws IOException  
  59.     {  
  60.         FileInputStream fis = new FileInputStream("D:\\abc\\1.mp3");  
  61.   
  62.         FileOutputStream fos = null;  
  63.   
  64.         byte[] buf = new byte[1024*1024];  
  65.   
  66.         int len = 0;  
  67.         int count = 1;  
  68.         while ((len=fis.read(buf))!=-1)  
  69.         {  
  70.             fos = new FileOutputStream("D:\\abc\\a\\"+(count++)+".part");  
  71.             fos.write(buf,0,len);  
  72.             fos.close();  
  73.         }  
  74.         fis.close();  
  75.     }  
  76. }  

对象的序列化
序列化
将对象的状态信息转换为可以存储或传输的形式的过程。在序列化期间,
对象将其当前状态写入到临时或持久性存储区。
以后,可以通过从存储区中读取或反序列化对象的状态,重新创建该对象。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.io.*;  
  2. class ObjectStreamDemo_4  
  3. {  
  4.     public static void main(String[] args) throws Exception  
  5.     {  
  6.           
  7.         //writeObj();  
  8.         readObj();  
  9.     }  
  10.   
  11.     //将存储到持久性存储介质中的对象取出  
  12.     public static void readObj() throws Exception  
  13.     {  
  14.         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));  
  15.   
  16.         Person p = (Person)ois.readObject();  
  17.   
  18.         System.out.println(p);  
  19.   
  20.         ois.close();  
  21.     }  
  22.   
  23.   
  24.     //将对象存储到持久性存储介质中  
  25.     public static void writeObj() throws IOException  
  26.     {  
  27.         //创建使对象序列化的对象  
  28.         ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));  
  29.         //将对象存储  
  30.         oos.writeObject(new Person("lisi",39));  
  31.   
  32.         oos.close();  
  33.     }  
  34. }  

管道流
先执行读取线程,但是没有数据,所以读取线程处于等待状态,
之后执行写入线程,将数据写入到读取线程的管道里,所以读取线程就可以读到数据了

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //读取线程  
  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.         //读取数据  
  12.         try  
  13.         {  
  14.             byte[] buf = new byte[1024];  
  15.   
  16.             System.out.println("读取前。。。没有数据,阻塞");  
  17.   
  18.             int len = in.read(buf);  //read方法没有数据是就是等待  
  19.   
  20.             System.out.println("读到数据。。。阻塞结束");  
  21.   
  22.             String s = new String(buf,0,len);  
  23.   
  24.             System.out.println(s);  
  25.   
  26.             in.close();  
  27.         }  
  28.         catch (IOException e)  
  29.         {  
  30.             throw new RuntimeException("管道读取流失败");  
  31.         }  
  32.     }  
  33. }  
  34.   
  35.   
  36. //写入线程  
  37. class Write implements Runnable  
  38. {  
  39.     private PipedOutputStream out;  
  40.     Write(PipedOutputStream out)  
  41.     {  
  42.         this.out = out;  
  43.     }  
  44.     public void run()  
  45.     {  
  46.         //写入数据  
  47.         try  
  48.         {  
  49.             System.out.println("开始写入数据前,等待6秒。。。");  
  50.             Thread.sleep(6000);  
  51.             out.write("piped lai le".getBytes());//getBytes,转换为字节流  
  52.             out.close();  
  53.         }  
  54.         catch (Exception e)  
  55.         {  
  56.             throw new RuntimeException("管道输出流失败");  
  57.         }  
  58.     }  
  59. }  
  60. class PipedStreamDemo_5   
  61. {  
  62.     public static void main(String[] args) throws IOException  
  63.     {  
  64.         PipedInputStream in = new PipedInputStream();  
  65.         PipedOutputStream out = new PipedOutputStream();  
  66.         in.connect(out);  
  67.   
  68.         Read r = new Read(in);  
  69.         Write w = new Write(out);  
  70.         new Thread(r).start();  
  71.         new Thread(w).start();  
  72.     }  
  73. }  

RandomAccessFile随机读写访问


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


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


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


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


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


主要应用:实现多线程下载

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.io.*;  
  2. class RandomAccessFileDemo_6   
  3. {  
  4.     public static void main(String[] args) throws IOException  
  5.     {  
  6.         //writeFile();  
  7.         readFile();  
  8.     }  
  9.       
  10.     //读取数据  
  11.     public static void readFile() throws IOException  
  12.     {  
  13.         RandomAccessFile raf = new RandomAccessFile("ran.txt","r");  
  14.   
  15.         //调整对象中指针  
  16.         //raf.seek(8*1);  
  17.   
  18.         //跳过指定的字节数,可以往下跳,不能往回走  
  19.         raf.skipBytes(8);  
  20.           
  21.         byte[] buf = new byte[4];  
  22.         raf.read(buf);  
  23.   
  24.         String name = new String(buf);  
  25.         int age = raf.readInt();  
  26.   
  27.         System.out.println("name"+name);  
  28.         System.out.println("age"+age);  
  29.   
  30.         raf.close();  
  31.     }  
  32.       
  33.     //任意位置插入数据  
  34.     public static void writeFile_2() throws IOException  
  35.     {  
  36.         RandomAccessFile raf = new RandomAccessFile("ran.txt","rw");  
  37.   
  38.         raf.write("周七".getBytes());  
  39.         raf.writeInt(100);  
  40.   
  41.         raf.close();  
  42.     }  
  43.   
  44.     //以读写模式写入数据  
  45.     public static void writeFile() throws IOException  
  46.     {  
  47.         RandomAccessFile raf = new RandomAccessFile("ran.txt","rw");  
  48.           
  49.         raf.write("李四".getBytes());  
  50.         raf.writeInt(97);  
  51.         raf.write("王五".getBytes());  
  52.         raf.writeInt(99);  
  53.   
  54.         raf.close();  
  55.     }  
  56. }  

DataInputStream与DataOutputStream


可以用于操作基本数据类型的数据的流对象。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.io.*;  
  2. class DataStreamDemo_7   
  3. {  
  4.     public static void main(String[] args)   
  5.     {  
  6.         System.out.println("Hello World!");  
  7.     }  
  8.       
  9.     //使用专用的uft-8读取数据  
  10.     public static void readUTFDemo() throws IOException   
  11.     {  
  12.         DataInputStream dis = new DataInputStream(new FileInputStream("utfdate.txt"));  
  13.   
  14.         String s = dis.readUTF();  
  15.   
  16.         System.out.println(s);  
  17.   
  18.         dis.close();  
  19.     }  
  20.   
  21.       
  22.     //使用专用的uft-8写入数据  
  23.     public static void writeUTFDemo() throws IOException  
  24.     {  
  25.         DataOutputStream dos = new DataOutputStream(new FileOutputStream("utfdate.txt"));  
  26.           
  27.         //使用这种方法写入的数据,只有用对应的方法才能取出  
  28.         dos.writeUTF("你好");  
  29.   
  30.         dos.close();  
  31.     }  
  32.       
  33.     //读取数据  
  34.     public static void readData() throws IOException  
  35.     {  
  36.         DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));  
  37.   
  38.         int num = dis.readInt();  
  39.         boolean b = dis.readBoolean();  
  40.         double d = dis.readDouble();  
  41.   
  42.         System.out.println("num="+num);  
  43.         System.out.println("b="+b);  
  44.         System.out.println("d="+d);  
  45.   
  46.         dis.close();  
  47.     }  
  48.   
  49.     //写入数据  
  50.     public static void writeData() throws IOException  
  51.     {  
  52.         DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));  
  53.   
  54.         dos.writeInt(234);  
  55.         dos.writeBoolean(true);  
  56.         dos.writeDouble(6465.325);  
  57.   
  58.         dos.close();  
  59.     }  
  60. }  

转换流的字符编码

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.io.*;  
  2. class EncodeStream_9  
  3. {  
  4.     public static void main(String[] args) throws IOException  
  5.     {  
  6.         System.out.println("Hello World!");  
  7.     }  
  8.       
  9.     //以特定编码表读取文件  
  10.     public static void readText() throws IOException  
  11.     {  
  12.         InputStreamReader isr = new InputStreamReader(new FileInputStream("utf.txt"),"gbk");  
  13.   
  14.         char[] buf = new char[10];  
  15.         int len = isr.read(buf);  
  16.   
  17.         String str = new String(buf,0,len);  
  18.   
  19.         System.out.println(str);  
  20.   
  21.         isr.close();  
  22.     }  
  23.   
  24.     //以特定编码表写入文件  
  25.     public static void writeText() throws IOException  
  26.     {  
  27.         OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("utf.txt"),"UTF-8");  
  28.   
  29.         osw.write("你好");  
  30.   
  31.         osw.close();  
  32.     }  
  33. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值