zoukankan      html  css  js  c++  java
  • JAVA IO 字符流 FileReader FileWriter

    摘抄自 b站尚硅谷JAVA视频教程

    FileReader fileReader = new FileReader(file)

    read()方法的使用:

     File file = new File("hello.txt");
            FileReader fileReader = null;
            try{
    
                 fileReader= new FileReader(file);
                int data;
                while((data=fileReader.read())!=-1){
                    System.out.print((char)data);
                }
            } catch (IOException e){
                e.printStackTrace();
            }finally {
                if(fileReader!=null){
                    try{
                        fileReader.close();
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }
            }

    read(char *)的使用方法

    File file = new File("hello.txt");
            FileReader fileReader = null;
            try{
    
                fileReader= new FileReader(file);
                char [] data = new char[5];
                int len=-1;
                while((len=fileReader.read(data))!=-1){
                    System.out.print(new String(data,0,len));
                }
            } catch (IOException e){
                e.printStackTrace();
            }finally {
                if(fileReader!=null){
                    try{
                        fileReader.close();
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }
            }

    write(file,append=true/false)方法

    若没有file不存在,则创建

    若file存在

      append=true:追加内容

      append=false:覆盖内容(默认)

    File file = null;
            file = new File("hi.txt");
            FileWriter fw=null;
            try {
                fw = new FileWriter(file);
                fw.write("hello world");
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(fw!=null){
                    try{
                        fw.close();
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }
            }
  • 相关阅读:
    javaEE的三层结构:web层、service层、dao层
    shell 流水账
    Git笔记(流水账)
    Openstack搭建(流水账)
    shell数组脚本
    linux配置邮箱服务
    Linux产生随机数的几种方法
    MySQL主从复制原理及配置过程
    安装并配置多实例Mysql数据库
    Nginx防盗链配置
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/12340692.html
Copyright © 2011-2022 走看看