Java IO流相关知识

一、IO流的分类
  • 字符流
                Reader
                        InputStreamReader(节点流)
                        BufferedReader(处理流)
                Writer
                        OutputStreamWriter(节点流)
                        BufferedWriter(处理流)
                        PrintWriter
  • 字节流
                InputStream
                        FileInputStream(节点流)
                        BufferedInputStream(处理流)
                        ObjectInputStream(处理流)
                        PrintStream
                OutputStream
                        FileOutputStream(节点流)
                        BufferedOutputStream(处理流)
                        ObjectOutputStream(处理流)
  •  断点处理的流
                RandomAccessfile
                
二、IO流的用法
1、转换流的用法
             FileInputStream in = new FileInputStream( new File( "" ));         
           Reader reader = new InputStreamReader( in ); //字节转字符
           FileOutputStream out = new FileOutputStream( new File( "" ));
           Writer writer = new OutputStreamWriter( out ); //字符转字节
           2、对象序列化,对象需要实现Serializable接口
               FileOutputStream fileOutputStream = new FileOutputStream( "C:\\Users\\lx\\Desktop\\Record.txt" );
            ObjectOutputStream objectOutputStream = new ObjectOutputStream( fileOutputStream );
            objectOutputStream .writeObject(object );//向指定文件写入对象object
            objectOutputStream .close();

               FileInputStream fileInputStream = new FileInputStream( "C:\\Users\\lx\\Desktop\\Record.txt" );
           ObjectInputStream objectInputStream = new ObjectInputStream( fileInputStream );
           object  =   objectInputStream .readObject();//读取得到对象object
               fileInputStream . lose();
        3、断点的运用
public class Copy extends Thread{  //可以利用多线程实现拷贝    
      long start ;
      long end ;
     File sorce ;
     File targetdir ;
     
      public Copy() {                  }
      public Copy( long start , long end , File sorce , File targetdir ) {//利用构造方法传递需要拷贝的长度,拷贝开始位置,以及目标文件和源文件
            super ();
            this . start = start ;
            this . end = end ;
            this . sorce = sorce ;
            this . targetdir = targetdir ;
     }
      @Override
      public void run(){         
            try {
                 RandomAccessFile souceRaf = new RandomAccessFile( sorce , "r" );
                RandomAccessFile targetRaf = new RandomAccessFile( new File( targetdir , sorce .getName()), "rw" );
                 souceRaf .seek( start );
                 targetRaf .seek( start );
                
                 int len = 0;
                 byte [] bs = new byte [1024];
                 long seek ;
                
                System. out .println( start + "---->" + end + this .getName());
                 while (( len = souceRaf .read( bs ))!=-1){
                      targetRaf .write( bs , 0, len );
           
                      seek = souceRaf .getFilePointer();//获取断点位置
                      if ( seek == end ){
                            break ;
                     }
                }    
                 targetRaf .close();
                 souceRaf .close();
                
           } catch (IOException e ) {
                 e .printStackTrace();
           }  
     }
}
4、字节流的用法
public class Test_InputStream {//利用字节流获取文本文件内容,但是容易出现问题
/*    
    //可能出现int长度越界
    public static void main(String[] args) throws IOException {
        InputStream inputStream = new FileInputStream(new File("C:\\Users\\lx\\Desktop\\test\\33.txt"));
        byte[] b = new byte[inputStream.available()];    
        inputStream.read(b);
        
        String str = new String(b);
        System.out.println(str);
    
    }
*/
    //可能出现乱码        
    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\lx\\Desktop\\test\\33.txt");
        InputStream inputStream = new FileInputStream(file);
        //统计每次读取的实际长度
        int len = 0;
        //声明每次读取1024个字节
        byte[] b = new byte[2];
        StringBuffer sBuffer = new StringBuffer();        
        while((len=inputStream.read(b))!=-1){
            sBuffer.append(new String(b,0,len));
        }
        System.out.println(sBuffer.toString());
    }
}
//利用字节流拷贝文件
public void copy(File sourceFile , File targetDir ) {//
           FileInputStream fileInputStream = null ;
           FileOutputStream fileOutputStream = null ;
            fileInputStream = new FileInputStream( sourceFile );
           File targetFile = new File( targetDir , sourceFile .getName());
            fileOutputStream = new FileOutputStream( targetFile );
            byte [] b = new byte [1024];
             int len = 0;
             while (( len = fileInputStream .read ( b )) != -1) {
                   fileOutputStream .write( b , 0, len );
             }

}
5、缓存字符流的用法
      public static void main(String[] args ) throws IOException {//缓存字符流实现写入文件
           InputStream in = System. in ;
           Reader reader = new InputStreamReader( in );
           BufferedReader br = new BufferedReader( reader );
           BufferedWriter bw = new BufferedWriter( new FileWriter( new File( "src/1.txt" )));
           String s = "" ;
            while (( s = br .readLine())!= null ) {
                 bw .write( s );
                 bw .newLine();
                  bw .flush();           //字符流千万不要忘了flush!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
           }
     }



猜你喜欢

转载自blog.csdn.net/qq_24065713/article/details/76407833
今日推荐