0

Is it possible in Android, to manually add a file inside a project and then, modify it? Example:

"I have a test.txt file in the following path: "app/src/data". I would like to make a method to write a given String in the test.txt file."

Is that possible? I been looking everywhere, but can't seen to do such an easy task.

Alain Cruz
  • 4,757
  • 3
  • 25
  • 43
  • Why do you want to do that .What I know is that you can use FileOutput/InputStream classes to read and write or even edit files but that is on the device itself not in your project files which are compressed to APK. – Omar Boshra May 24 '17 at 23:09
  • I was very confused regarding the way Android saved external files. Now that I read a bit more, I finally understood what I was looking for. Thanks for the comment. – Alain Cruz May 24 '17 at 23:35

1 Answers1

2

If you mean modifying files inside the APK itself then it's not possible. Besides, the folder structure you see in the project is not the final structure on the APK (just unzip your APK, it's a .ZIP really): Fpr example, the source directory is all compiled into a classes.dex. The res/ directory is compiled and fully copied ...

Take a look at How to write files to assets folder or raw folder in android?

and https://developer.android.com/training/basics/data-storage/files.html

You can read raw files stored in /res/raw, or assets stored in assets/ , but you cannot modify stuff inside the APK itself.

What you can do is create and modify as many files as you wish from the different places Android gives to any app, such as:

  • CACHE directory (context.getCacheDir() -> /sdcard/Android/data/your.package/cache

  • External files (context.getExternalFilesDir() -> /sdcard/Android/data/your.package/files

  • Arbitrary directories in the SDCARD.
rupps
  • 9,712
  • 4
  • 55
  • 95
  • 1
    Thanks a lot for the explanation. I am new to Android and didn't understand how files were stored. Now I have more understanding on this subject. – Alain Cruz May 24 '17 at 23:36