6

I have an application that make me choose a file through a file explorer(the file is stored on the sd), and then reads it.

I want to modify it, so it has the file directly into the app and reads the file from "inside". Where I have to put the file into the project? How can I access it?

1
  • You can add the file to assets folder at the time of building your app.. but cannot write in to assets folder later on after the application i build up... the way you can write on SD Card Commented Apr 2, 2013 at 7:42

3 Answers 3

4

You can save a file inside your project by using the following code:

 File cDir = getApplication().getExternalFilesDir(null); File saveFilePath = new File(cDir.getPath() + "/" + "yourfilename"); 

You can see the saved file inside "files" folder of your application package name in your device.

Try the following path in your device:

File manager >> Android >> data >> "your package name" >> files >> new file. 
Sign up to request clarification or add additional context in comments.

1 Comment

This is the perfect solution for store file inside the package. User will not interact directly with file
0

Yes, you can put your file into /assets folder, and retrieve as follows:

AssetManager assetManager = getAssets(); InputStream instream = assetManager.open("file.txt"); 

or res/raw folder:

InputStream raw = getResources().openRawResource(R.raw.file); 

If you want to modify it, you'll be able only to write a file into External storage (e.g. sdcard), or into Internal storage (under your application folder data/data/package_name/). If you store your file into External storage it will persist until user manually or programmatically deletes the file. But if you store this file into Internal storage, it will be deleted if user deletes an app, or clear an application cache.

1 Comment

I think there is limit on number of bytes readable from assets folder.
0

Demo

File myExternalFile; if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) { saveToExternalStorage.setEnabled(false); } else { myExternalFile = new File(getExternalFilesDir(filepath), filename); } 

save External Storage (FileOutputStream )

try { FileOutputStream fos = new FileOutputStream(myExternalFile); fos.write(myInputText.getText().toString().getBytes()); fos.close(); } catch (IOException e) { e.printStackTrace(); } myInputText.setText(""); responseText.setText("Saved to External Storage.(StorageFile.txt)"); 

Get External Storage (FileInputStream )

try { FileInputStream fis = new FileInputStream(myExternalFile); BufferedReader br = new BufferedReader( new InputStreamReader(fis)); String strLine; while ((strLine = br.readLine()) != null) { myData = myData + strLine; } in.close(); } catch (IOException e) { e.printStackTrace(); } myInputText.setText(myData); responseText .setText("Data retrieved from Internal Storage.(StorageFile.txt)"); 

3 Comments

Please don't use DataInputStream to read a text file. You don't need it so please remove it as people might copy this code.
@PeterLawrey: Then which class should use ?
Use the other classes you have already. DataInputStream is redundant and this bad example is copied around 30x every month on stack overflow.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.