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?

hmjd
  • 120,187
  • 20
  • 207
  • 252
gabskoro
  • 175
  • 1
  • 2
  • 7
  • I think you'll have to read it in, and write a new file. I'd be interested if someone has another solution though. – FoamyGuy Feb 20 '12 at 21:21
  • Maybe you want http://developer.android.com/reference/java/io/RandomAccessFile.html – Sparky Feb 20 '12 at 21:28

3 Answers3

1

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.

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • Yes, I know about RandomAccessFile, but that's what you say, it simple overwrite the text, so i can't use this :/ – gabskoro Feb 21 '12 at 10:33
  • I was trying to explain that you can easily `extend` either `FileWriter` or `RandomAccessFile` and implement your own 'prepend' method which reads the current contents, appends them to the new line and then writes them back to the file. I was also trying to explain that that is how ANY file writer that can do this would have to work. – Squonk Feb 21 '12 at 20:34
1

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?

Choker
  • 875
  • 6
  • 9
  • Yes I am controlling the view, and now I am reversing the content, but I am making a new update in my application, and display an other view using WebView, and in WebView, and it's to difficult to arrange the data in the cells, when the data is reversed.
    – gabskoro Feb 21 '12 at 10:41
0

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

gabskoro
  • 175
  • 1
  • 2
  • 7