• 字符流五种读写 字符流 BufferedWriter BufferedReader 带缓冲区的字符流


    字符流五种读写

     1 package test03;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.BufferedWriter;
     5 import java.io.File;
     6 import java.io.FileNotFoundException;
     7 import java.io.FileReader;
     8 import java.io.FileWriter;
     9 import java.io.IOException;
    10 
    11 import javax.annotation.processing.Filer;
    12 
    13 public class CopyfileDemo {
    14     public static void main(String[] args) throws Exception {
    15         File src = new File("a.txt");
    16         File dest = new File("b.txt");
    17         copy1(src,dest);
    18         copy2(src,dest);
    19         copy3(src,dest);
    20         copy4(src,dest);
    21         copy5(src,dest);
    22     }
    23     //缓冲区字符流 特殊方法
    24     private static void copy5(File src, File dest) throws IOException {
    25         // TODO Auto-generated method stub
    26         BufferedReader br = new  BufferedReader(new FileReader(src));
    27         BufferedWriter bw = new BufferedWriter(new FileWriter(dest));
    28         
    29         String line = null;
    30         while((line  = br.readLine())!=null) {//一次读一行
    31             bw.write(line);
    32             bw.newLine();//换行
    33         }
    34         br.close();
    35         bw.close();
    36         
    37     }
    38     //带缓冲区字符流一次一个字符数组
    39     private static void copy4(File src, File dest) throws IOException {
    40         // TODO Auto-generated method stub
    41         BufferedReader br = new  BufferedReader(new FileReader(src));
    42         BufferedWriter bw = new BufferedWriter(new FileWriter(dest));
    43         int  len = 0;
    44         char[] chs = new  char[1024];
    45         while((len = br.read(chs))!=-1) {
    46             bw.write(chs, 0, len);
    47         }
    48         br.close();
    49         bw.close();
    50     }
    51     //缓冲区字符流 一次读一个字符
    52     private static void copy3(File src, File dest) throws Exception {
    53         // TODO Auto-generated method stub
    54         BufferedReader br = new  BufferedReader(new FileReader(src));
    55         BufferedWriter bw = new BufferedWriter(new FileWriter(dest));
    56         
    57         int  b = 0;
    58         while((b = br.read())!=-1) {
    59             bw.write(b);
    60         }
    61         br.close();
    62         bw.close();
    63         
    64         
    65     }
    66     //普通字符流 一次读写一个字符数组
    67     private static void copy2(File src, File dest) throws IOException {
    68         // TODO Auto-generated method stub
    69         FileReader fr  =new FileReader(src);
    70         FileWriter fw = new FileWriter(dest);
    71         
    72         int len = 0;
    73         char [] chs = new  char[1024];
    74         while((len = fr.read(chs))!=-1) {
    75             fw.write(chs,0,len);
    76         }
    77         fr.close();
    78         fw.close();
    79         
    80     }
    81     //普通字符流  一次读写一个字符
    82     private static void copy1(File src, File dest) throws IOException {
    83         // TODO Auto-generated method stub
    84         FileReader fr = new FileReader(src);
    85         FileWriter fw = new FileWriter(dest);
    86         
    87         int b = 0;
    88         while((b = fr.read())!=-1) {
    89             fw.write(b);
    90         }
    91         
    92         fr.close();
    93         fw.close();
    94         
    95         
    96     }
    97 }
    字符流五种读写方式

    特殊方法

     BufferedReader

    String readLine()
              读取一个文本行。
     1 public class BufferedReaderDemo2 {
     2     public static void main(String[] args) throws IOException {
     3         BufferedReader br = new BufferedReader(new FileReader("a.txt"));
     4         
     5         //一次读取一行
     6         /*String s = br.readLine();
     7         System.out.println(s);*/
     8         
     9         String line = null;
    10         while((line = br.readLine())!=null) {
    11             System.out.println(line);
    12         }    
    13         br.close();
    14     }
    15 }

    BufferedWriter

    void newLine()
              写入一个行分隔符。
    public static void main(String[] args) throws IOException {
            BufferedWriter bw = new  BufferedWriter(new FileWriter("a.txt"));
            bw.write(97);
            bw.write("helloword");
            bw.write("
    ");//换行符
            bw.newLine();//换行
            
            char[] ch = {'1','2','u','g','h','d'};
            bw.write(ch);
            bw.newLine();//换行
            
            bw.write("helloword");
            
            bw.close();
        }

  • 相关阅读:
    Android getMeasuredHeight()与getHeight()的区别
    Android控件属性android:visibility的"invisible"与"gone"的区别
    浅谈Android onClick与onLongClick事件触发的问题
    Android ListView中FooterView布局问题
    Android GridView 问题
    Android HTTP POST上传
    监听EditText实时输入
    C++03下的delegate实现-
    delegate委托的C++实现--C++11/14(原创)
    Unity3D 移动平台实现一种大规模(其实跟PC比还是算小规模)动画角色渲染的方案---绝对原创方案。。。
  • 原文地址:https://www.cnblogs.com/star521/p/8950107.html
Copyright © 2020-2023  润新知