码迷,mamicode.com
首页 > 其他好文 > 详细

流的关联关系

时间:2020-04-05 18:38:39      阅读:55      评论:0      收藏:0      [点我收藏+]

标签:top   auto   string+   trace   dwr   stat   static   writer   nts   

更多应用案例:

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

流的关联关系

标签:top   auto   string+   trace   dwr   stat   static   writer   nts   

原文地址:https://www.cnblogs.com/macro-renzhansheng/p/12638157.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!