JAVA之IO字符流的基础应用

public class IO_字符流 {
    public static void main(String[] args) throws Exception{
        Reader r = new FileReader("文件路径");
        BufferedReader br=new BufferedReader(r);//缓冲流
//        char[] c = new char[1024];//定义一个桶装每次读取多少个数据
//        int len;
//        while ((len = br.read(c)) != -1) {//读取所有内容,如果读完所有内容则停止
//            String str = new String(c, 0, len);//每次读取0到len的所有内容
//            System.out.print(str);//因为读取时会自动换行所以这里我们不需要换行
//        }
        String line;
        while((line=br.readLine())!=null){//一次读取一行
            System.out.println(line);//因为读取一行时程序不会自己换行,所以这里我们需要给它换行
        }
        }
    }
    class IO_写入{
        public static void main(String[] args) throws Exception{
            FileWriter fw=new FileWriter("文件路径",true);//后面加true是追加的意思,不加true则每次写入都会覆盖原有的内容
            BufferedWriter bw=new BufferedWriter(fw);//缓冲流
            bw.write("嗨!\n");
            //bw.flush();//刷新
            bw.close();
//            String str="大家好";
//            fw.write(str);
//            fw.write("\n");//将输出语句换行
//            fw.close();
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_62731133/article/details/124179921