zoukankan      html  css  js  c++  java
  • java存储数据到本地txt文件中

    java存储数据,方便打印日志等

    1、会覆盖以前的数据

    try {
    
        File writeName = new File("D:\data.txt"); // 相对路径,如果没有则要建立一个新的output.txt文件
        if(!writeName.exists()) {
            writeName.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
        }
        FileWriter writer = new FileWriter(writeName);
        BufferedWriter out = new BufferedWriter(writer);
        out.write(data);
        out.flush(); // 把缓存区内容压入文件
    out.close();
    } catch (IOException e) { e.printStackTrace(); }

    2、接下一行存储,数据不覆盖  

    try {
            File file = new File("D:\data.txt");
            if(!file.exists()) {
                file.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
            }
            FileOutputStream fos = new FileOutputStream(file,true);
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write(data);
            bw.newLine();
            bw.flush();
            bw.close();
            osw.close();
            fos.close();
    }catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } catch (IOException e2) {
        e2.printStackTrace();
    }
  • 相关阅读:
    Redis事务和锁
    11/6笔记 补充(Redis持久化,RDB&&AOF)
    11/6随笔
    Redis 安装教程
    Redis通用指令和第一个Jedis程序的实现
    Redis学习第二天
    SpringBoot学习笔记
    1000行代码手写服务器
    log4j创建实例异常
    寒假阅读人月神话3
  • 原文地址:https://www.cnblogs.com/qiantao/p/11804925.html
Copyright © 2011-2022 走看看