Use FileWriter to enter text at the beginning

Hy, I use FileWriter to write text on an SD card like

 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 looks like this

 This is first written This is second writtn 

Now, my question is: how can I write this second text and place it above the first text, I did not find any search option. I know that I can read the text first, and not write a new file, and finally read the text, but is there another solution?

+4
source share
3 answers

I don't know which default class is used in Android, which will do this.

Even if there is one, then if you think about it, the only possible way it can function is to do what you say about the first reading of the current content, add to the data to be added, and then write it back.

You can create a class that extends FileWriter , which does this by first opening / reading / closing the file, and then reopening it with the append set to false before writing new data with the previous data added to it.

The sincere idea of ​​using RandomAccessFile is an alternative, because you could at least strive to RandomAccessFile before writing data (eliminating the need to close the file before reopening it). BUT you still need to first read the previous data, as the search for the beginning, and then write will not "push" the existing data to the side - it will simply overwrite it.

+1
source

Do you control the presentation of the file, unlike the user using any other program to view it? If so, how to simply display the contents of the file in reverse order?

+1
source

I will explain it to you here.

Say I wrote some data about 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 application I read the text and break it into <#>

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

I want to display text using WebView and display data in a table that looks like this.

 ---------------------- | 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 the row, and when the date is equal to the date before in the same table, close the table and open a new one and put in new data and read again and see if the date is equal to the date of the data before putting it in the same table, the others close again and open a new table ... it's not so difficult to build, but to create and return it is difficult to eliminate: D, so I want to change the data in the file, so I don’t have to worry with the opposite sign, simpy reads the data and creates the table

0
source

Source: https://habr.com/ru/post/1397498/


All Articles