Giter Club home page Giter Club logo

sd's Introduction

sd

dsa import java.io.*;

public class Main { /** * 文件追加数据 * 原理就是将原文件数据先写进缓冲区,再在缓冲区追加数据最后保存 * 也可以重新创建个备份文件记录原文件,再记录进缓冲区 * 这里用第二种 * 要用第一种,就只需要用一个字符串变量来记录备份,但是万一突然断电,缓冲区所有记录消失,还是更安全点好 * @param args * @throws Exception */ static void file_add(String str) throws Exception { //首先创建一个备份文件,并读取原文件写入备份数据 File c = new File("C:/Users/神和五律/Desktop/c.txt"); BufferedWriter c_w = new BufferedWriter(new FileWriter("C:/Users/神和五律/Desktop/c.txt")); BufferedReader a_r = new BufferedReader(new FileReader("C:/Users/神和五律/Desktop/a.txt")); String a_copy; while((a_copy=a_r.readLine())!=null) { c_w.write(a_copy); c_w.write("\n"); } c_w.close();

    //再用备份文件先写入原文件缓冲区
    BufferedReader c_r = new BufferedReader(new FileReader("C:/Users/神和五律/Desktop/c.txt"));
    String c_str;
    BufferedWriter a_w = new BufferedWriter(new FileWriter("C:/Users/神和五律/Desktop/a.txt"));
    while((c_str=c_r.readLine())!=null) {
        a_w.write(c_str);
        a_w.write("\n");
    }
    
    //先不要写入,再把需要追加的参数数据写入缓冲区
    a_w .write(str);
    
    //然后写入
    a_w.close();
    
    //最后删除备份文件(也可以保留),删除需要中止该文件的所有数据流,最好所有的都结束
    a_r.close();
    a_w.close();
    c_r.close();
    c_w.close();
    c.delete();
    
}

public static void main(String[] args) throws Exception{
    file_add("ababababababab");
}

}

sd's People

Contributors

londies avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.