4

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?

4

3 回答 3

1

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

于 2012-02-20T21:45:46.600 回答
1

我不知道默认情况下 Android 中有一个可以执行此操作的类。

即使当时有一个,如果您考虑一下,它唯一可能起作用的方法就是按照您所说的先读取当前内容,附加到要添加的数据,然后再写回。

您可以创建一个扩展的类,该类FileWriter首先打开/读取/关闭文件,然后使用appendset to再次打开它,然后再false写入新数据并附加以前的数据。

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

于 2012-02-20T21:39:09.860 回答
0

我将在这里向您解释。

假设我在文件上写了一些数据,数据看起来

 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所以我想更改文件中的数据,这样我就不必为倒车而烦恼, simpy 读取数据并创建表

于 2012-02-21T10:56:43.833 回答