JAvaにおけるIOの変換フローとバッファフロー


01変換フローの概要
* A:      
    * a:      
        * OutputStreamWriter             :           ,              
        *                 ,              
02変換フロー_文字のバイト変換プロセス
* A:    _        
    * a.  
        *   day24_source/   .JPG  
03 OutputStreamWriter書き込みファイル
* A: OutputStreamWriter     
    * a: OutputStreamWriter
        * java.io.OutputStreamWriter   Writer 
        *          ,     
        * write()  ,    ,       
        *          ,        
        * OutputStreamWriter     
            *     :
                * OutputStreamWriter(OuputStream out)          
                *      :  FileOutputStream       
                * OutputStreamWriter(OutputStream out, String charsetName)
                * String charsetName         GBK  UTF-8 
        * OutputStreamWriter     ,  FileWriter
    * b:     

            public class OutputStreamWriterDemo {
                public static void main(String[] args)throws IOException {
            //        writeGBK();
                    writeUTF();
                }
                /*
                 *      OutputStreamWriter   
                 *   UTF-8     
                 */
                public static void writeUTF()throws IOException{
                    //       ,    
                    FileOutputStream fos = new FileOutputStream("c:\\utf.txt");
                    //       ,           ,       UTF-8
                    OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
                    osw.write("  ");
                    osw.close();
                }

                /*
                 *       OutputStreamWriter   
                 *     GBK     
                 */
                public static void writeGBK()throws IOException{
                    //       ,      
                    FileOutputStream fos = new FileOutputStream("c:\\gbk.txt");
                    //       ,    ,       ,  GBK   
                    OutputStreamWriter osw = new OutputStreamWriter(fos);
                    //      
                    osw.write("  ");

                    osw.close();
                }
            }
04変換フロー_バイト文字ストリームプロシージャ
* A:    _        
    * a: InputStreamReader            
        * java.io.InputStreamReader    Reader
        *      ,      
        *          ,        
        *      :
            * read()   1   ,      
        *   
            * OuputStreamWriter    
            * InputStreamReader    
        * OutputStreamWriter(OutputStream out)       
        * InputStreamReader(InputStream in)           
        *           : FileInputStream
        * InputStreamReader(InputStream in,String charsetName)         
    * b:   
        *   day24_source/   .JPG  
05 InputSteamReaderテキストファイルの読み込み
* A: InputSteamReader      
    * a:     
        public class InputStreamReaderDemo {
            public static void main(String[] args) throws IOException {
        //        readGBK();
                readUTF();
            }
            /*
             *     ,InputSteamReader    
             *    UTF-8   ,    utf
             */
            public static void readUTF()throws IOException{
                //       ,      
                FileInputStream fis = new FileInputStream("c:\\utf.txt");
                //       ,     ,       ,       
                InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
                char[] ch = new char[1024];
                int len = isr.read(ch);
                System.out.println(new String(ch,0,len));
                isr.close();
            }
            /*
             *     ,InputSteamReader    
             *           ,  GBK  
             */
            public static void readGBK()throws IOException{
                //       ,      
                FileInputStream fis = new FileInputStream("c:\\gbk.txt");
                //       ,    ,       
                InputStreamReader isr = new InputStreamReader(fis);
                char[] ch = new char[1024];
                int len = isr.read(ch);
                System.out.println(new String(ch,0,len));

                isr.close();
            }
        }
06変換ストリームの子の親の違い
* A:           
    * a:     
        OutputStreamWriter:
            |--FileWriter:
        InputStreamReader:
            |--FileReader;
    * b:   
        * OutputStreamWriter InputStreamReader         :           。       :   +   。
        * FileWriter FileReader:    ,               。
                    ,                ,            ,     。
        *          
            * InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));//     。
            * InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"GBK");//  GBK   。
            * FileReader fr = new FileReader("a.txt");
07バッファフローの概要
* A:      
    * a:   
        *    IO      
        *               
08バイト出力ストリームBufferedOutputStream
* A:         BufferedOutputStream
    * a: BufferedOutputStream
        *          
        * java.io.BufferedOuputStream   :             
        * BufferedOuputStream    OutputStream
        *   ,   write   ,                 
        *     :
            * BufferedOuputStream(OuputStream out)
            *             ,          ,             

    * b:     
        public class BufferedOutputStreamDemo {
            public static void main(String[] args)throws IOException {
                //       ,    
                //FileOutputStream fos = new FileOutputStream("c:\\buffer.txt");
                //             ,     ,       
                BufferedOutputStream bos = new
                        BufferedOutputStream(new FileOutputStream("c:\\buffer.txt"));

                bos.write(55);

                byte[] bytes = "HelloWorld".getBytes();

                bos.write(bytes);

                bos.write(bytes, 3, 2);

                bos.close();
            }
        }
09バイト入力ストリームバッファストリームBufferedInputStream
* A:         BufferedInputStream
    * a: BufferedInputStream
        *          
        *   InputStream,        
        *       read()     ,                  
        *     :
            * BufferedInputStream(InputStream in)
            *             ,    ,       
            *            FileInputStream
    * b:     
        public class BufferedInputStreamDemo {
            public static void main(String[] args) throws IOException{
                //             ,            ,    
                BufferedInputStream bis = new 
                        BufferedInputStream(new FileInputStream("c:\\buffer.txt"));
                byte[] bytes = new byte[10];
                int len = 0 ;
                while((len = bis.read(bytes))!=-1){
                    System.out.print(new String(bytes,0,len));
                }
                bis.close();
            }
        }
10 4種類のファイルコピー方式の効率比較
* A:             
    * a:       
        *                              125250   
        *                              193        OK
        *                          1210     
        *                          73         OK        

    * b:     

        public class Copy {
            public static void main(String[] args)throws IOException {
                long s = System.currentTimeMillis();
                copy_4(new File("c:\\q.exe"), new File("d:\\q.exe"));
                long e = System.currentTimeMillis();
                System.out.println(e-s);
            }
            /*
             *   ,      
             *  4.              
             */
            public static void copy_4(File src,File desc)throws IOException{
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
                int len = 0 ;
                byte[] bytes = new byte[1024];
                while((len = bis.read(bytes))!=-1){
                    bos.write(bytes,0,len);
                }
                bos.close();
                bis.close();
            }
            /*
             *   ,      
             *  3.              
             */
            public static void copy_3(File src,File desc)throws IOException{
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
                int len = 0 ;
                while((len = bis.read())!=-1){
                    bos.write(len);
                }
                bos.close();
                bis.close();
            }

            /*
             *   ,      
             *  2.          
             */
            public static void copy_2(File src,File desc)throws IOException{
                FileInputStream fis = new FileInputStream(src);
                FileOutputStream fos = new FileOutputStream(desc);
                int len = 0 ;
                byte[] bytes = new byte[1024];
                while((len = fis.read(bytes))!=-1){
                    fos.write(bytes,0,len);
                }
                fos.close();
                fis.close();
            }

            /*
             *   ,      
             *  1.          
             */
            public static void copy_1(File src,File desc)throws IOException{
                FileInputStream fis = new FileInputStream(src);
                FileOutputStream fos = new FileOutputStream(desc);
                int len = 0 ;
                while((len = fis.read())!=-1){
                    fos.write(len);
                }
                fos.close();
                fis.close();
            }
        }
11文字出力ストリームバッファフローBufferedWriter
* A:         BufferedWriter
    * a: BufferedWriter
        *          
        * java.io.BufferedWriter    Writer
        *      write ()     ,    ,   

        *     :
            * BufferedWriter(Writer w)         
            *    ,    
            *           FileWriter, OutputStreamWriter
    * b:     
        public class BufferedWrierDemo {
            public static void main(String[] args) throws IOException{
                //       ,    
                FileWriter fw = new FileWriter("c:\\buffer.txt");
                BufferedWriter bfw = new BufferedWriter(fw);

                bfw.write(100);
                bfw.flush();
                bfw.write("  ".toCharArray());
                bfw.flush();


                bfw.write("  ");

                bfw.flush();


                bfw.write("   ");

                bfw.flush();

                bfw.write("    ");
                bfw.flush();

                bfw.close();

            }
        }
12文字出力ストリームBufferedWriter固有メソッドnewLine
* A:         BufferedWriter    newLine
    * a:     
        * void  newLine()    

        * newLine()     , \r
* * Windows \r
* Linux
* newLine() , * JVM: Windows ,newLine() \r
* Linux ,newLine()
/* * c:\\a.txt * d:\\a.txt * , * , * * , 1 * , 1 */ * b: public class BufferedWrierDemo { public static void main(String[] args) throws IOException{ // , FileWriter fw = new FileWriter("c:\\buffer.txt"); BufferedWriter bfw = new BufferedWriter(fw); bfw.write(100); bfw.flush(); bfw.write(" ".toCharArray()); bfw.flush(); bfw.write(" "); bfw.newLine(); bfw.flush(); bfw.write(" "); bfw.newLine(); bfw.flush(); bfw.write(" "); bfw.flush(); bfw.close(); } }
13文字入力ストリームバッファフローBufferedReader
* A:         BufferedReader
    * a:   
        *            ,      ,      、         
        * public String readLine()        ,          ,         ,        ,    null
14文字入力ストリームBufferedReaderテキスト行の読み込み
* A:         BufferedReader     
    * a: BufferedReader
        *         
        * java.io.BufferedReader    Reader
        *      read()     ,    
        *     :
            * BufferedReader(Reader r)
            *           
               FileReader  InputStreamReader       
        * BufferedReader     
            * String readLine()       \r
* , null * b: * * int * null * boolean false * c: public class BufferedReaderDemo { public static void main(String[] args) throws IOException { int lineNumber = 0; // , , BufferedReader bfr = new BufferedReader(new FileReader("c:\\a.txt")); // readLine() // , readLine() null String line = null; while((line = bfr.readLine())!=null){ lineNumber++; System.out.println(lineNumber+" "+line); } bfr.close(); } } /* * String line = bfr.readLine(); System.out.println(line); line = bfr.readLine(); System.out.println(line); line = bfr.readLine(); System.out.println(line); line = bfr.readLine(); System.out.println(line); line = bfr.readLine(); System.out.println(line); */
15文字ストリームバッファストリームコピーテキストファイル
* A:              
    * a:     
        /*
         *          ,      
         *       BufferedReader+FileReader   
         *       BufferedWriter+FileWriter   
         *       ,    ,   ,   
         */
        public class Copy_1 {
            public static void main(String[] args) throws IOException{
                BufferedReader bfr = new BufferedReader(new FileReader("c:\\w.log"));    
                BufferedWriter bfw = new BufferedWriter(new FileWriter("d:\\w.log"));
                //     ,    ,   ,   
                String line = null;
                while((line = bfr.readLine())!=null){
                    bfw.write(line);
                    bfw.newLine();
                    bfw.flush();
                }
                bfw.close();
                bfr.close();
            }
        }
16 IOストリームオブジェクトの操作規則
* A: IO        
    * a:    :                。
        *  :InputStream    Reader
        *   :OutputStream Writer
        *          ,    。

    * b:    :              ?
        *  :
            *   :InputStream
            *   :Reader
        *   :
            *   :OutputStream
            *   :Writer
    * c:    :           。
        *    :
            *   :    File  。
            *   :  ,   。
            *   :System.in;
            *   :Socket
        *     :
            *   :    File  。
            *   :  ,   。
            *   :System.out
            *   :Socket
            *                 。
    * d:    :         ?
        *     :
            *    ?   。InputStreamReader OutputStreamWriter
            *    ?     。BufferedXXX
            *             。