How can I edit the text files in assets folder in Android? - android

I am using a text file in the assets folder in Android. I would like to change the data inside that text file dynamically. I am trying to open the file as follows:
FileOutputStream fos=this.getAssets().openNonAssetFd("data.txt").createOutputStream();
But it is generating the error: java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed. Please help me to edit this file. Thank you very much.

Writing into /assets directory at runtime? AFAIK that's not possible.
You can put the original file in /assets, and at the first application run copy it over to the /sdcard.

Related

Referencing files in Android

Why can't I refer to a PDF file in my Android device?
I have a folder named "templates" in the root folder, and I have a PDF file in that. I've tried to refer to the file via the following:
File file = new File("/templates/myPDFFile.pdf");
but when I try to print file.exists(), it always returns me false.
Just as further information, my file hierarchy is:
root
-src
-com.example.text
-MyActivity.java
-templates
-MyPDFFile.pdf
and my code is stored in MyActivity.java.
How do I reference accurately to the PDF file?
You must put it in assets and get it from there like :
How to open a pdf stored either in res/raw or assets folder?

I get file not found exception trying to open a linked file in my assets folder (android)

I have a file in my assests folder, that I imported using the 'link' option (as opposed to copy). I use the following code to open it, but I get a file not found exception.
super.onCreate(savedInstanceState);
setContentView(R.layout.bar_page_lo);
appContext = getApplicationContext();
InputStream ins;
ins = appContext.getResources().getAssets().open("bar-data.json");
I need it to be linked because the file is in a dropbox folder so that my colleague can edit it. is there something special I need to do to use a linked file?
try:
appContext.getResources().openRawResource(R.raw.bar-data);
File must be placed in raw folder.

How to open and read a file in the src/my/package folder?

I would like to load the contents of a text file in a String.
The text file should stay in the same folder src/my/package as the .java.
However, I can't find the path to this file:
I have tried:
File f = new File("src/my/package/file.js");
File f = new File("file.js");
and many others but nothing worked.
What is the correct path to my file?
To open files located on the classpath, use the Class.getResource() family of methods. They work even if the file is inside a jar file with your classes. So something like
InputStream is = getClass().getResourceAsStream("/my/package/file.js");

Problems reading file from ddms in android?

The file i want to open is located in the file:
/data/data/MY_PACKAGE/files/samplefile.txt
I have pulled the file from ddms and the desired content is in it
I just want to get the text file in order to parse it..
I tried the following:
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File xmlFile = new File(sdcard,"samplefile.txt");
but no joy...
Thanks in advance!!
Use getFilesDir(), not Environment.getExternalStorageDirectory(), to get at the root of internal storage, where your file resides.
In order to open a file stored within your applications private directory you must use openFileInput(String fileName). This will return a FileInputStream that you can then work with.

File not found issue in Android

I need to access myfile.txt file using FileReader in Android , please suggest me where to add the text file in Eclipse. I tried it adding it in Resource and Asset but I am getting File not found issue.
FileReader fr = new FileReader("myfile.txt");
Even
File ff = new File("myfile.txt");
File Supports only the below listed parameters
FileReader Supports only the below listed parameters
Note: I want solution for this issue , only with FileReader or File
The directory would be /res/raw/ this is where you put all your extra resources.
you can refer to it using getResources().openRawResource(resourceName)
and check here Android how to get access to raw resources that i put in res folder?
EDIT:
how can i edit the text files in assets folder in android
in short
the easiest way would be to copy the file to external directory then do your stuff there
link is here
Android: How to create a directory on the SD Card and copy files from /res/raw to it?
One thing to mention - prior to 2.3 the file size in the assets cannot exceed 1MB.
hope it helps abit
That's how I obtain my file from the SD card, perhaps this can be some use to you.
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File options = new File(getAppDirectory(), "portal.xml");
}
The getAppDirectory method used in the bit of code looks like this :
private String getAppDirectory() {
return new String(Environment.getExternalStorageDirectory().toString()
+ "/foldername/foldername/");
}
After this bit of code I also make sure the file exists and what not before I attempt to read from it.

Categories

Resources