Java中常用的输入输出流

来源:百度文库 编辑:神马文学网 时间:2024/04/20 04:31:17

1. 抽象类InputStream\OutputStream

    方法read(), write(), close()

    流结束的判断方法 read()的返回值为-1, readLine()返回值为null

2. 文件读写类FileInputStrea\FileOutputStream

    方法: read()方法将文件读入一个byte类型的数组,其数组长度可以由in.avalialbe()方法获得

    read(byte[], int off, int len) off指从流中读入的字节所放入数组中的开始数字, len指读入长度

    write(byte[], int off, int len) off指定数组的起始位置,从该位置起的字节写入流中,len指写入长度

    byte数组的最大长度为60M,如超出则需要将文件分段

Java代码
  1. package example;   
  2. import java.io.*;   
  3.   
  4. public class IOTest {   
  5.   
  6.     /**  
  7.      * @param args  
  8.      */  
  9.     public static void main(String[] args) {   
  10.         // TODO Auto-generated method stub   
  11.         File inFile = new File("C:\\test\\1.jpg");   
  12.         File outFile = new File("C:\\test\\2.jpg");   
  13.            
  14.         if(!outFile.exists()){   
  15.             try{   
  16.                 outFile.createNewFile();   
  17.             }   
  18.             catch(IOException ex){   
  19.                 ex.printStackTrace();   
  20.             }   
  21.         }   
  22.         try{   
  23.             FileInputStream in = new FileInputStream(inFile);   
  24.             byte[] array = new byte[in.available()];   
  25.             in.read(array);   
  26.             FileOutputStream out = new FileOutputStream(outFile);   
  27.             out.write(array);   
  28.             in.close();   
  29.             out.close();   
  30.         }   
  31.         catch(FileNotFoundException ex){   
  32.             ex.printStackTrace();   
  33.         }   
  34.         catch(IOException ex){   
  35.             ex.printStackTrace();   
  36.         }   
  37.            
  38.   
  39.     }   
  40.   
  41. }  

3. 管道流PipedInputStream/PiledOutputStream

    管道的建立有两种构造方式:

    1) PipedInputStream pInput = new PipedInputStream();

        PipedOutputStream pOutput = new PipedOutputStream();

    2) PipedInputStream pInput = new PipedInputStream();

        PipedOutputStream pOutput = new PipedOutputStream();

        pInput.connect(pOutput);

Java代码
  1. package example;   
  2. import java.io.*;   
  3.   
  4. public class PipeTest {   
  5.   
  6.     /**  
  7.      * @param args  
  8.      */  
  9.     public static void main(String[] args) {   
  10.         // TODO Auto-generated method stub   
  11.         PipedInputStream pipeIn = new PipedInputStream();   
  12.         PipedOutputStream pipeOut = new PipedOutputStream();   
  13.         try{   
  14.             File f1 = new File("C:\\test\\1.txt");   
  15.             File f2 = new File("C:\\test\\2.txt");   
  16.             FileInputStream fileIn = new FileInputStream(f1);   
  17.             pipeOut.connect(pipeIn);   
  18.             Write writer = new Write(fileIn,pipeOut);   
  19.             writer.start();            
  20.             //byte temp[] = new byte[pipeIn.available()];   
  21.             InputStreamReader inReader = new InputStreamReader(pipeIn,"GBK");   
  22.             BufferedReader reader = new BufferedReader(inReader);   
  23.             char temp[] = new char[10];   
  24.             /* Read test 1  
  25.             char temp[] = new char[20];  
  26.             reader.read(temp);  
  27.             String s = new String(temp);  
  28.             System.out.println(temp);  
  29.             */  
  30.             int c= reader.read(temp);   
  31.             while(c!=-1){   
  32.                 System.out.println(temp);   
  33.                 c= reader.read(temp);   
  34.             }   
  35.                
  36.         }   
  37.         catch(IOException ex){   
  38.             ex.printStackTrace();   
  39.         }   
  40.   
  41.     }   
  42. package example;   
  43. import java.io.*;   
  44.   
  45. public class Write extends Thread{   
  46.     FileInputStream fileIn;   
  47.     PipedOutputStream pipedOut;   
  48.     int c =0;   
  49.     public Write(FileInputStream fileIn,PipedOutputStream pipeOut){   
  50.         this.fileIn = fileIn;   
  51.         this.pipedOut = pipeOut;   
  52.     }   
  53.     public void run(){   
  54.         try{   
  55.             for (int i = 0; i<25; i++){   
  56.                  c = fileIn.read();   
  57.                  pipedOut.write(c);   
  58.             }   
  59.             sleep(5000);   
  60.             for (int i = 0; i<10; i++){   
  61.                  c = fileIn.read();   
  62.                  pipedOut.write(c);   
  63.             }   
  64.         }   
  65.         catch(IOException ex){   
  66.             ex.printStackTrace();   
  67.         }   
  68.         catch(InterruptedException ex){   
  69.             ex.printStackTrace();   
  70.         }   
  71.     }   
  72.   
  73. }   
  74.     
  75. }  

   在上面的代码中,遇到的问题是,写进程sleep一段时间之后再次写入的值无法被读进程读取