流的关联关系

隐身守侯 提交于 2020-04-05 18:13:39

更多应用案例:

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() {

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!