Fastest way to write to file?

后端 未结 6 1897
难免孤独
难免孤独 2020-12-02 15:54

I made a method that takes a File and a String. It replaces the file with a new file with that string as its contents.

This is what I made

6条回答
  •  旧时难觅i
    2020-12-02 16:25

    In Java, the BufferWriter is very slow: Use the native methods directly, and call them as little as possible (give them as much data per call as you can).

        try{
            FileOutputStream file=new FileOutputStream(file);
            file.write(content);
            file.close();
        }catch(Throwable e){
            D.error(e);
        }//try
    

    Also, deleting the file can take a while (maybe it is being copied to the recycle bin first). Just overwrite the file, like in the above code.

提交回复
热议问题