Blank file after writing to it?

前端 未结 3 1450
旧巷少年郎
旧巷少年郎 2021-01-18 02:54

So I have been trying to write a Bukkit plugin for a friend of mine, and for some reason the config generation is not working. The code in question is below, and I will be h

相关标签:
3条回答
  • 2021-01-18 03:22

    You need to call...

    out.flush();
    

    ... right before you call...

    out.close();
    

    PrintWriter uses buffer in memory to make more efficient use of the disk. You need to call flush() to tell the PrintWriter to write your data.

    0 讨论(0)
  • 2021-01-18 03:25

    Most likely you have run this more than once and you are writing to one file and checking another. I suspect the filenames are the same but the directory, perhaps based on the working directory is not what you think it is.

    Close calls flush in this situation as PrintWriter uses BufferedWriter and the other Writer class are not buffered.

      251       public void flush() throws IOException {
      252           synchronized (lock) {
      253               flushBuffer();
      254               out.flush();
      255           }
      256       }
      257   
      258       public void close() throws IOException {
      259           synchronized (lock) {
      260               if (out == null) {
      261                   return;
      262               }
      263               try {
      264                   flushBuffer();
      265               } finally {
      266                   out.close();
      267                   out = null;
      268                   cb = null;
      269               }
      270           }
      271       }
    
    0 讨论(0)
  • 2021-01-18 03:30

    You need to flush or close the stream when you're done. Both are very important before exiting. Close will automatically call flush().

    0 讨论(0)
提交回复
热议问题