码迷,mamicode.com
首页 > 编程语言 > 详细

java _io_字符缓冲流的输入、输出

时间:2019-07-26 19:34:58      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:users   换行符   work   buffer   内容   write   file   use   buffere   

新增方法:
输入:
BufferedReader breader=new BufferedReader(Reader reader);
.readLine()读取一行内容,返回字符串
输出:
BufferedWriter bwriter=new BufferedWirter(Writer writer);
.newLine() 写入一个换行符

输入:
File f =new File("C:\Users\10853\eclipse-workspace\hell\src\hell\abc");
BufferedReader reader=null;
try {

        reader=new BufferedReader(new FileReader(f));

        String line=null;
        while((line=reader.readLine())!=null)//要添上!=null,不像c语言赋值表达式整个也有个真假结果
        {
            System.out.println(line);
        }
    }catch(FileNotFoundException e)
    {
        e.printStackTrace();
    }catch(IOException e)
    {
        e.printStackTrace();
    }finally {

        try {
            if(null!=reader)
            {
                reader.close();
            }
        }catch(IOException e)
        {
            e.printStackTrace();
        }
    }

输出:

    File f=new File("D:d/c.txt");

    BufferedWriter bwriter=null;
    try
    {
        bwriter=new BufferedWriter(new FileWriter(f));

        String s="addad";
        bwriter.write(s);
        bwriter.newLine();
        bwriter.append("aaaff");
        bwriter.flush();
    }catch(FileNotFoundException e)
    {
        e.printStackTrace();
    }catch(IOException e)
    {
        e.printStackTrace();
    }finally {
        try {
            if(null!=bwriter)
            {
                bwriter.close();
            }
        }catch(IOException e)
        {
            e.printStackTrace();
        }   
    }

java _io_字符缓冲流的输入、输出

标签:users   换行符   work   buffer   内容   write   file   use   buffere   

原文地址:https://blog.51cto.com/14437184/2423916

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