Java读写文件的几种方式及程序

Java把这些不同来源和目标的数据都统一抽象为数据流。Java语言的输入输出功能是十分强大而灵活的。在Java类库中,IO部分的内容是很庞大的,有许多不同的方式进行文件读写。
下面简单介绍解中文件的读写方式。

1.BufferedReader/BuffererdWritter

public List<String> readFromFile(String filePath) {
    try {
      File file = new File(filePath);
      //如果文件不存在
      if (!file.exists()) {
        return null;
      }
      FileReader fr = new FileReader(filePath);
      BufferedReader br = new BufferedReader(fr);
      String str;
      while ((str = br.readLine()) != null) {
	      System.out.println(str);
      }
      //关闭文件
      br.close();
      return strList;
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }
  }

//由于当时需要把文字分开,所以传入一个List,如果需要的话可以替换为需要的参数
public boolean writeFile(List<String> strList, String filePath) {
    try {
      File file = new File(filePath);
      //文件不存在
      if (!file.exists()) {
        file.createNewFile();
      }
      FileWriter fw = new FileWriter(file.getAbsoluteFile());
      BufferedWriter bw = new BufferedWriter(fw);
      if (strList == null) {
        bw.close();
        return false;
      }
      for (String str : strList) {
        bw.write(str + "\n");
      }
      bw.flush();
      bw.close();
      System.out.println("Done");
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }

2.FileInputStream / FileOutputStream

public List<String> readFromFile(String filePath) {
    FileInputStream fip = null;
    File file;
    try {
      file = new File(filePath);
      fip = new FileInputStream(file);
      if (!file.exists()) {
        fip.close();
        return null;
      }
      byte[] strReadByte = new byte[(int) file.length()];
      fip.read(strReadByte);
      fip.close();
      char[] strReadChar = new String(strReadByte).toCharArray();
      //格式化
      List<String> strList = new ArrayList<String>();
      String tmp = "";
      for (char c : strReadChar) {
        if (c == '\n') {
          if (tmp != "") {
            strList.add(tmp);
          }
          tmp = "";
        } else {
          tmp += c;
        }
      }
      return strList;
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }
  }
  
public boolean writeFile(List<String> strList, String filePath) {
    FileOutputStream fop = null;
    File file;
    try {
      file = new File(filePath);
      fop = new FileOutputStream(file);
      if (!file.exists()) {
        file.createNewFile();
      }
      if (strList == null) {
        fop.close();
        return false;
      }
      for (String str : strList) {
        fop.write((str + "\n").getBytes());
      }
      fop.flush();
      fop.close();
      System.out.println("Done");
    } catch (IOException e) {
      e.printStackTrace();
    } 
    return true;
  }

3.FileReader / FileWriter

public List<String> readFromFile(String filePath) {
    try {
      List<String> strList = new ArrayList<String>();
      File file = new File(filePath);
      if (!file.exists()) {
        return null;
      }
      FileReader fileReader = new FileReader(filePath);
      char[] strReadChar = new char[(int) file.length()];
      fileReader.read(strReadChar);
      //格式化
      String tmp = "";
      for (char c : strReadChar) {
        if (c == '\n') {
          strList.add(tmp);
          tmp = "";
        } else {
          tmp += c;
        }
      }
      fileReader.close();
      return strList;
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }
  }
  
public boolean writeFile(List<String> strList, String filePath) {
    try {
      //读入文件
      File file = new File(filePath);
      //若文件不存在则创建
      if (!file.exists()) {
        file.createNewFile();
      }
      FileWriter fileWritter = new FileWriter(filePath);
      if (strList == null) {
        fileWritter.close();
        return false;
      }
      for (String str : strList) {
        fileWritter.write(str+"\n");
      }
      fileWritter.flush();
      fileWritter.close();
      System.out.println("Done");
    } catch (IOException e) {
      e.printStackTrace();
    }
    return true;
  }


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值