• 文件读写操作


    作者QQ:1095737364    QQ群:123300273     欢迎加入!
     
    1.读取一行放入到List数组里面:
    List<String> list = new ArrayList<String>();
    File file = new File("F:/email.txt");
    BufferedReader reader = null;
    try {
      reader = new BufferedReader(new FileReader(file));
      String tempString = null;
      // 一次读入一行,直到读入null为文件结束
      while ((tempString = reader.readLine()) != null) {
        list.add(tempString);
      }
      reader.close();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (IOException e1) {
        }
      }
    }
    2.写入一行文字到文件
    String tempString="";//要写入的数据
    File file = new File("F:/email_suffx.txt");
    try {
      //如果文件不存在就新建
      if(!file.exists()){
        file.createNewFile();
      }
      FileOutputStream out=new FileOutputStream(file,true); //如果追加方式用true
      out.write((tempString+"
    ").getBytes("utf-8"));//注意需要转换对应的字符集
      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  • 相关阅读:
    循环语句
    流程控制
    特殊的赋值运算符
    位运算符
    运算符
    八种基本类型
    cmd基础命令
    springboot项目部署到tomcat步骤以及常见问题
    【算法问题】如何实现大整数相加
    【算法问题】删除k个数字后的最小值
  • 原文地址:https://www.cnblogs.com/yysbolg/p/7346466.html
Copyright © 2020-2023  润新知