I'm developing an application for save more accounts. I don't use database but i use file... My question is if is possible change or delete a single row in the file txt on the storage of my phone???
Asked
Active
Viewed 46 times
-1
-
I've successfully written and read files. It's probably possible to update them as well. The files are stored in /data/data/com.example.yourapp/files (be sure to replace com.example.yourapp with your package name). To view the files on disk go to View > Tool Windows > Device File Explorer in Android Studio. – Michael Osofsky May 08 '19 at 19:16
-
but I want delete or change with the Java Code .-. – Alex Casale May 08 '19 at 19:29
-
Yes of course, as long as your read write access to the file. – Ali Ben Zarrouk May 08 '19 at 20:05
1 Answers
1
You can edit a file only if you have read and write access to it. You mentioned in the comments that you have already read and written to a file. To edit what you have to do is read the content from the file, edit it and write it again. To achieve that just repeat what you did when reading/writing to it
To edit a file's content, you need to read it from disk, create a new FileInputStream with it, edit your stream then create a new FileOutPutSteam and write to it, like below:
File file = f;
FileInputStream fin;
fin = new FileInputStream(file);
byte fileContent[] = new byte[(int)file.length()];
fin.read(fileContent)
//Convert your stream to string, manipulate it and convert it back to a stream
FileOutputStream fos = new FileOutputStream(f.getAbsolutePath());
fos.write(my_out_stream);
fin.close();
fos.close();
To convert your FileInputStream to a string use: https://stackoverflow.com/a/15161590/10089348
And to convert back it to a FileOutputStream use https://stackoverflow.com/a/4069932/10089348
Hope it helps :)

Rander Gabriel
- 637
- 4
- 14