流的关联关系

更多应用案例:

BufferedReader bufferedReader = new BufferedReader(
                     new InputStreamReader(
                             new FileInputStream(
                                     new File("C:\\Users\\86156\\Desktop\\HelloWorld.java")
                                     )));


BufferedWriter bWriter = new BufferedWriter(
                     new OutputStreamWriter(
                             new FileOutputStream(
                                     new File("C:\\Users\\86156\\Desktop\\HelloWorld_buffer2.txt"))));


InputStreamReader inputStreamReader = new InputStreamReader(
                     new FileInputStream(
                             new File("C:\\Users\\86156\\Desktop\\HelloWorld.java")
                     ));

OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
                     new FileOutputStream(
                             new File("C:\\Users\\86156\\Desktop\\HelloWorld_char.txt")
                             ));


FileInputStream fileInputStream = new FileInputStream(
                     new File("C:\\Users\\86156\\Desktop\\HelloWorld.java"));


FileOutputStream fileOutputStream = new FileOutputStream(
                     new File("C:\\Users\\86156\\Desktop\\HelloWorld.txt")

实现文件地读写

public static void bufferStream_array() {
         //StringBuffer sBuffer = new StringBuffer();
         String[] strings = new String[50];
         try {
             BufferedReader bufferedReader = new BufferedReader(
                     new InputStreamReader(
                             new FileInputStream(
                                     new File("C:\\Users\\86156\\Desktop\\HelloWorld.java")
                                     )));
            
             String tempString = null;
             try {
                 int index = 0;
                 while ((tempString = bufferedReader.readLine())!=null) {
                     strings[index] = tempString+"\r\n";
                     index ++;
                    
                 }
             } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }
            
         } catch (FileNotFoundException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
         //System.out.println(Arrays.toString(strings));
         try {
             BufferedWriter bWriter = new BufferedWriter(
                     new OutputStreamWriter(
                             new FileOutputStream(
                                     new File("C:\\Users\\86156\\Desktop\\HelloWorld_buffer2.txt"))));
             try {
                 int index = 0;
                 do {
                     System.out.println(strings[index]);
                     bWriter.write(strings[index]);
                     bWriter.flush();
                     index ++;
                 } while (index<strings.length && strings[index]!=null);
                
                 bWriter.close();
                
                 /*
                  * System.out.println("缓冲数据:"+sBuffer);
                  * System.out.println("缓冲数据:"+sBuffer.toString());
                  */
                 System.out.println("缓冲流写出成功");
             } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }
         } catch (FileNotFoundException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
     }
    
     public static void bufferStream() {
         StringBuffer sBuffer = new StringBuffer();
         try {
             BufferedReader bufferedReader = new BufferedReader(
                     new InputStreamReader(
                             new FileInputStream(
                                     new File("C:\\Users\\86156\\Desktop\\HelloWorld.java")
                                     )));
            
             String tempString = null;
             try {
                 while ((tempString = bufferedReader.readLine())!=null) {
                     sBuffer.append(tempString);
                     sBuffer.append("\n");
                 }
             } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }
            
         } catch (FileNotFoundException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
        
         try {
             BufferedWriter bWriter = new BufferedWriter(
                     new OutputStreamWriter(
                             new FileOutputStream(
                                     new File("C:\\Users\\86156\\Desktop\\HelloWorld_buffer.txt"))));
             try {
                 bWriter.write(sBuffer.toString());
                 bWriter.flush();
                 bWriter.close();
                 /*
                  * System.out.println("缓冲数据:"+sBuffer);
                  * System.out.println("缓冲数据:"+sBuffer.toString());
                  */
                 System.out.println("缓冲流写出成功");
             } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }
         } catch (FileNotFoundException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
     }
    
     public static void charStream() {
         char[] cbuf = new char[1024];
         try {
             InputStreamReader inputStreamReader = new InputStreamReader(
                     new FileInputStream(
                             new File("C:\\Users\\86156\\Desktop\\HelloWorld.java")
                     ));
            
             try {
                 inputStreamReader.read(cbuf);
                 inputStreamReader.close();
             } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }
            
         } catch (FileNotFoundException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
         try {
             OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
                     new FileOutputStream(
                             new File("C:\\Users\\86156\\Desktop\\HelloWorld_char.txt")
                             ));
             //char[] cbuf = new char[1024];
             try {
                 outputStreamWriter.write(cbuf);
                 outputStreamWriter.flush();
                 outputStreamWriter.close();
                 System.out.println("字符流写入成功");
             } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }
            
         } catch (FileNotFoundException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
     }
    
     public static void byteStream() {

posted @ 2020-04-05 17:35  小海_macro  阅读(188)  评论(0编辑  收藏  举报