java如何写入文件中_如何写入文件并在java中动态保存(How to write to a file and save it dynamically in java)...
发布日期:2021-06-24 11:32:03 浏览次数:2 分类:技术文章

本文共 3229 字,大约阅读时间需要 10 分钟。

如何写入文件并在java中动态保存(How to write to a file and save it dynamically in java)

我有一个要求写入文件而不是System.out.println并同时保存,以便用户可以通过打开文件随时查看进度。

现在我正在使用以下代码

FileWriter fstream = null;

File file1 = new File(logFile);

fstream = new FileWriter(file1);

out = new BufferedWriter(fstream);

out.write(msg);

out.write("\n");

但它不会将内容保存到文件中,除非我说Out.close();

仅供参考 - 1.这是一个Java EE应用程序代码。 2.我不想使用Log4jLogger

有没有更简单的方法来做到这一点? 任何指针?

I have a requirenment to write to a file rather than in System.out.println and save it at same time so that user can see the progress anytime by opening the file.

Right now I am using following code

FileWriter fstream = null;

File file1 = new File(logFile);

fstream = new FileWriter(file1);

out = new BufferedWriter(fstream);

out.write(msg);

out.write("\n");

but it does not save the contents to the file unless I say Out.close();

FYI - 1. This is a Java EE application code. 2. I donot want to user Log4jLogger

Is there any simpler way to do this? Any pointers?

原文:https://stackoverflow.com/questions/11198361

更新时间:2020-02-27 09:43

最满意答案

你不需要调用close() - 只需调用flush() 。

刷新此输出流并强制写出所有缓冲的输出字节。

You don't need to call close() - just call flush().

Flushes this output stream and forces any buffered output bytes to be written out.

2012-06-25

相关问答

你只能读取jar内的文件而不能写入它们。 You can only read from files inside a jar not write to them.

使用此构造函数: FileWriter(File file, boolean append)

“给定一个File对象构造一个FileWriter对象。” http://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html Use this constructor: FileWriter(File file, boolean append)

"Constructs a FileWriter object given a File

...

目前尚不清楚您的文件是否有多个标题。 假设您的文件只有一个标题,我们可以按照以下方式执行此操作 - 1.由于您的文件只包含一次标题,您可以检查文件是否第一次访问 - File f = new File(//your file name);

if(f.exists() && !f.isDirectory()) {

//write heading

}

2.如果首次访问该文件,则可以添加标题 - String heading = "Some heading";

完整的代码看起来像 - pub

...

如果你想写一个String数组,你最好用一个文本文件。 使用文本文件的优点是可以很容易地查看,编辑并且可以被系统中的许多其他工具使用,这意味着您不必自己编写这些工具。 您还可以发现简单的文本格式比使用XML或JSON更快,更紧凑。 注意:这些格式对于复杂的数据结构更加有用。 public static void writeArray(PrintStream ps, String... strings) {

for (String string : strings) {

as

...

不要使用ObjectOutputStream 。 直接使用FileOutputStream : FileOutputStream out=new FileOutputStream("SaveObj3.sav");

out.write(0);

out.close();

Don't use ObjectOutputStream. Use the FileOutputStream directly: FileOutputStream out=new FileOutputStream("SaveObj3

...

两件事: 请发布正确的代码。 XDocument类没有实例方法“parse”,只有“Parse”。 XDocument类没有实例方法“Load”,只有静态方法“Load”。 xdoc.Parse(content)将从字符串创建一个XDocument。 XDocument.Load(filename)将返回从XML文件“filename”加载的XDocument。 这可以做到这一点: foreach(DataRow dr in dt.Rows) {

string filepath = Con

...

你不需要调用close() - 只需调用flush() 。 刷新此输出流并强制写出所有缓冲的输出字节。 You don't need to call close() - just call flush(). Flushes this output stream and forces any buffered output bytes to be written out.

你需要flush()和close() FileWriter (关闭它也会刷新它)。 尝试这个 : do {

System.out.println("Enter 'q' for quit!!");

str=br.readLine();

FileWriter fw=new FileWriter(file,true);

bw=new BufferedWriter(fw);

bw.write(str);

bw.flush(); //do this ,

...

你可以使用偏好: Preferences prefs = Gdx.app.getPreferences("My Preferences");

prefs.putString("name", "Donald Duck");

String name = prefs.getString("name", "No name stored");

prefs.putBoolean("soundOn", true);

prefs.putInteger("highscore

...

转载地址:https://blog.csdn.net/weixin_32512381/article/details/114924532 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:php跳转图片_PHP:header详解-页面刷新,页面跳转…
下一篇:java swing中两种设置背景图 容器图片_Java Swing中两种设置背景图(容器图片) | 学步园...

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月07日 03时39分15秒