写入后的空白文件? [英] Blank file after writing to it?

查看:164
本文介绍了写入后的空白文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在尝试为我的一个朋友写一个Bukkit插件,由于某种原因,配置生成不起作用。有问题的代码如下,我很乐意添加人们需要的任何代码来帮助我解决这个问题。当我运行该程序时,创建的配置文件最终为空白。测试文件很好(我通过简单地注释删除文件的行来测试)但是一旦我尝试获得多行就失败了。任何人都可以帮忙吗?

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 happy to add any code that people need to help me out with this. When I run the program, the config file that was created ends up blank. The testing file is just fine (I tested that by simply commenting out the line that deletes the file) but once I try to get multiple lines it fails. Can anyone help?

PrintWriter out = new PrintWriter(new FileWriter(config));
out.println("########################################");
out.println("#  hPlugin is written by newbiedoodle  #");
out.println("########################################");
out.println("#   -----  Plugin config file  -----   #");
out.println("########################################");
out.println("NOTE: Do not edit the config file besides changing the values - it may result in errors.");
out.println("--------------");
out.println("Strikes before being banned?");
out.println("3");
out.println("Godmode?");
out.println("true");
out.println("First time running the plugin?");
out.println("true");
out.println("Curse protection?");
out.println("true");
out.println("Emergency shelter?");
out.println("true");
out.println("Path building?");
out.println("true");
out.println("Blocked words/phrases (Separate with comma)");
out.println("[censored]");
out.close();
System.out.println("Successfully wrote defaults to config");

整个事情都包含在try / catch循环中,只是为了捕获可能弹出的任何错误。我觉得我错过了一些非常明显的东西,但我找不到它是什么。

The whole thing is enclosed in a try/catch loop just to catch any errors that may pop up. I get a feeling that I am missing something extremely obvious, but I can't find what it is.


  • config 是一个文件对象及其需要的路径,所以我认为不是那个

  • config is a File object with the path that it needs to have, so I don't think that it's that

我让程序分别执行几乎所有操作,以便我可以准确地告诉用户错误发生的位置,因此文件是在之外创建的尝试 / catch

I have the program do almost everything separately so that I can tell the user exactly where an error happened, so the file is created just outside of the try/catch.

推荐答案

您需要致电...

out.flush();

...在你打电话之前......

... right before you call...

out.close();

PrintWriter在内存中使用缓冲区来更有效地使用磁盘。您需要调用flush()来告诉PrintWriter写入您的数据。

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.

这篇关于写入后的空白文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆