使用 FileWriter 在开始时写入文本

发布于 2025-01-07 06:19:32 字数 651 浏览 2 评论 0原文

嘿,我正在使用 FileWriter 在 SD 卡上写入一些文本,就像这样

FileWriter write = new FileWriter(Environment.getExternalStorageDirectory().toString() + "text.txt", true);
write.append("This is first written");
write.close();

现在当我写入一些其他文本

FileWriter write = new FileWriter(Environment.getExternalStorageDirectory().toString() + "text.txt", true);
write.append("This is second written");
write.close();

并查看这个文件时,它看起来是这样

This is first written
This is second writtn

现在,我的问题是,我该如何写这个第二个文本并将其放置在第一个文本之上,我还没有找到任何搜索选项。我知道我可以先读取文本,然后编写一个新文件并最后放入读取的文本,但是还有其他解决方案吗?

Hy, I am using FileWriter to write some text on SD Card, like so

FileWriter write = new FileWriter(Environment.getExternalStorageDirectory().toString() + "text.txt", true);
write.append("This is first written");
write.close();

Now when i write some other text

FileWriter write = new FileWriter(Environment.getExternalStorageDirectory().toString() + "text.txt", true);
write.append("This is second written");
write.close();

And look in this file, it look so

This is first written
This is second writtn

Now, my question is, how can i write this second text and position it above the first text, I haven't found any seek option. I know that i can first read the text, and than write a new file and put at last the readen text, but is there any other solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

银河中√捞星星 2025-01-14 06:19:32

我不知道 Android 中默认有一个类可以做到这一点。

即使有,如果您考虑一下,它唯一可能的工作方式就是执行您所说的首先读取当前内容,附加到要添加的数据,然后写回。

您可以创建一个扩展 FileWriter 的类,该类通过首先打开/读取/关闭文件,然后将 append 设置为 false 再次打开它> 在写入新数据并附加之前的数据之前。

Sparky 使用 RandomAccessFile 的想法是一种替代方案,因为您至少可以在写入数据之前查找开头(无需在重新打开之前先关闭文件)。但是您仍然必须先读取以前的数据,因为寻找开头,然后写入不会“推开”现有数据 - 它只会覆盖它。

I'm not aware of a class available by default in Android that would do this.

Even if there is one then, if you think about it, the only possible way it could function is to do what you say about read current contents first, append to data to be added and then write back.

You could create a class which extends FileWriter which does this by first opening / reading / closing the file and then opens it again with append set to false before writing the new data with the previous data appended to it.

Sparky's idea of using a RandomAccessFile is an alternative because you could at least seek to the beginning before writing the data (removing the need to close the file first before reopening). BUT you would still have to read the previous data first as seeking to the beginning and then writing won't 'push' the existing data out of the way - it'll simply overwrite it.

我很OK 2025-01-14 06:19:32

您是控制文件视图的人,而不是用户使用其他程序来查看文件吗?如果是这样,只反向显示文件内容怎么样?

Are you the one controlling the view of the file, as opposed to the user using some other program to view it? If so, how about just display the file contents in reverse?

纸短情长 2025-01-14 06:19:32

我在这里给大家解释一下。

假设我已经在文件上写入了一些数据,并且数据

 21.02.2012, 11:43<#>-<#>88<#>-
 21.02.2012, 13:12<#>-<#>80<#>-
 21.02.2012, 19:36<#>-<#>61<#>-
 22.02.2012, 07:15<#>-<#>50<#>-
 23.02.2012, 12:14<#>-<#>44<#>-

现在看起来在我的应用程序中,我读取了文本并将其拆分为 <#>

 String split = readen.split("<#>");

我想使用 WebView 显示文本并显示表中的数据,因此

----------------------
| 23.02.2012         |
----------------------
| 12:14 | - | 44 | - |
----------------------

----------------------
| 22.02.2012         |
----------------------
| 07:15 | - | 50 | - |
----------------------

----------------------
| 21.02.2012         |
----------------------
| 19:36 | - | 61 | - |
| 13:12 | - | 80 | - |
| 11:43 | - | 88 | - |
----------------------

我读取一行,当日期等于之前的日期时,将其显示在同一个表中,否则关闭表并打开一个新表并放入新数据,然后再次阅读并查看日期日期是否等于放入同一个表之前的数据,其他再次关闭并打开一个新表...这并不难构建,但是构建它并恢复非常困难:D所以我想要更改文件中的数据,以便我不必费心反转,只需读取数据并创建表

I will explain it to you here.

Let's say i have written some data on a file, and the data looks

 21.02.2012, 11:43<#>-<#>88<#>-
 21.02.2012, 13:12<#>-<#>80<#>-
 21.02.2012, 19:36<#>-<#>61<#>-
 22.02.2012, 07:15<#>-<#>50<#>-
 23.02.2012, 12:14<#>-<#>44<#>-

now in my app i read the text and split it on <#>

 String split = readen.split("<#>");

i want to display the text using WebView and display the data in table that it looks so

----------------------
| 23.02.2012         |
----------------------
| 12:14 | - | 44 | - |
----------------------

----------------------
| 22.02.2012         |
----------------------
| 07:15 | - | 50 | - |
----------------------

----------------------
| 21.02.2012         |
----------------------
| 19:36 | - | 61 | - |
| 13:12 | - | 80 | - |
| 11:43 | - | 88 | - |
----------------------

I read a line and when the date equales the date before then it in the same table, else close the table and open a new and put the new data, and read again and see if date date equales data before put in the same table, other again close and open a new table...this is not so difficult to built, but to built this and revert is reaaaally difficult :D so i want to change the data in file, so that i dont have to bother with the reversing, simpy read the data and creating the table

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文