字符流Reader和Writer

Reader类常用的方法

  • int read()
  • int read(char[] c);
  • read(char[] c,int off, int len)
  • void close
    子类InputStreamReader常用的构造方法
  • InputStreamReader(InputStream in)
  • InputStreamReader(InputSream in,String charseName)
public static void main(String[] args) {
        System.out.println(readFile("E:/文件/test2.txt"));
    }
    public static String readFile(String path) {
        File f = new File(path);
        FileReader fr=null;
        String str=null;
        try {
            fr=new FileReader(f);
            char[] c = new char[(int) f.length()];
            fr.read(c);
            str = new String(c);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return str;
    }

BufferedReader

为了提高字符流读取文本文件的效率,我们可以使用BufferedReader类。BufferedReader类是Reader类的子类,它带有缓冲区,按行读取内容。

 public static void main(String[] args) {
        BufferedReader br=null;
        Reader fr=null;
        FileInputStream fis=null;
        try {
            fis=new FileInputStream("E:/文件/test3.txt");
            fr=new InputStreamReader(fis);
            br = new BufferedReader(fr);
            String str=null;
            while ((str = br.readLine()) != null) {
                System.out.println(str);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                br.close();
                fr.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

Writer类

Writer类常用方法:

  • write(String str)
  • write(String str,int off,int len)
  • void close()
  • void flush()

子类OutputStreamWriter常用的构造方法

  • OutputStreamWriter(OutputStream out)
  • OutputStreamWriter(OutputStream out,String charsetName)
public static void main(String[] args) {
        Writer fw = null;
        try {
            System.out.println(System.getProperty("file.encoding"));
            fw = new FileWriter("E:/文件/test3.txt");
            String str = "好好学习!";
            fw.write(str);
            fw.write(str, 0, 2);
            fw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

Writer类同样有BufferedWriter类

 public static void main(String[] args) {
        String str = "浔阳江头夜送客,枫叶荻花秋瑟瑟。主人下马客在船,举酒欲饮无管弦。醉不成欢惨将别,别时茫茫江浸月。";
        writeFiel(str, "E:/文件/test5.txt", false);
    }
    public static void writeFiel(String str, String path, boolean isAppend) {
        FileWriter fw=null;
        BufferedWriter bw = null;
        try {
            fw=new FileWriter(path,isAppend);
            bw = new BufferedWriter(fw);
            bw.write(str);
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值