Writing/Reading Files To/From Android Phone's Internal Memory

Writing/Reading Files to/from Android phone's internal memory

You can Read/ Write your File in data/data/package_name/files Folder by,

To Write

BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new 
File(getFilesDir()+File.separator+"MyFile.txt")));
bufferedWriter.write("lalit poptani");
bufferedWriter.close();

To Read

 BufferedReader bufferedReader = new BufferedReader(new FileReader(new 
File(getFilesDir()+File.separator+"MyFile.txt")));
String read;
StringBuilder builder = new StringBuilder("");

while((read = bufferedReader.readLine()) != null){
builder.append(read);
}
Log.d("Output", builder.toString());
bufferedReader.close();

Reading and writing to files in internal storage on Android

The File should be saved under /data/data/Android/urpackagename/ folder.

To read

 FileInputStream in = openFileInput("filename.txt");
InputStreamReader inputStreamReader = new InputStreamReader(in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}

For More Detail with same piece of code Write and Read openFile...() api

Android write text file to internal storage

Android apps are isolated one to another, so your app has a dedicated folder in internal storage to read/write into it.

You can access it via

File path = Environment.getDataDirectory();

As it vary between devices.

Outside the app (e.g. from a shell, or a file explorer) you can't even read private data folders of the apps, you need a rooted device (as the emulator) to do it. If you want a file to be world-readable, put it in the external storage.

Android: how to write a file to internal storage

Use the below code to write a file to internal storage:

public void writeFileOnInternalStorage(Context mcoContext, String sFileName, String sBody){      
File dir = new File(mcoContext.getFilesDir(), "mydir");
if(!dir.exists()){
dir.mkdir();
}

try {
File gpxfile = new File(dir, sFileName);
FileWriter writer = new FileWriter(gpxfile);
writer.append(sBody);
writer.flush();
writer.close();
} catch (Exception e){
e.printStackTrace();
}
}


Starting in API 19, you must ask for permission to write to storage.

You can add read and write permissions by adding the following code to AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

You can prompt the user for read/write permissions using:

requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE}, 1);

and then you can handle the result of the permission request in onRequestPermissionsResult() inside activity called from it.

Is it possible to read a file from internal storage (Android)?

Yes you can read file from internal storage.

for writing file you can use this

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

to read a file use the below:

To read a file from internal storage:

Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream. Read bytes from the file with read(). Then close the stream with close().

Code:

StringBuilder sb = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
is.close();
} catch(OutOfMemoryError om) {
om.printStackTrace();
} catch(Exception ex) {
ex.printStackTrace();
}
String result = sb.toString();

Refer this link

read/write some files from internal storage at runtime

I am developing a program that need read/write some files from sdcard or internal storage at runtime.

Here, and in your question title, you say you want to write to internal storage. Later, you say that you do not want to write to internal storage.

Which is it?

What I have to do for phones that have not card slot like Htc one x or phones that have not any sdcard inserted?

External storage != "sdcard". External storage can be whatever the device manufacturer wants, so long as it meets the terms of the Compatibility Definition Document. Hence, external storage can be removable (e.g., SD card) or not (e.g.,. dedicated portion of on-board flash). And, on Android 3.0+, external storage is merely a special subdirectory on the same partition that contains internal storage.

You only care about whether external storage is presently available or not. It should be available pretty much all of the time on Android 3.0+ devices. It will be unavailable on Android 1.x and 2.x devices if the device is plugged into a host computer and the host computer has mounted the device's external storage (e.g., as a drive letter on Windows).

if sdcard does not exists i realy don't know what i have to do.

You ask the user to please unmount their device from their host computer. Or, you decide to write to internal storage in those cases.

how to read file from the phone memory in android?

Here's how to write in a file..

FileOutputStream fos = openFileOutput("urls.txt", Context.MODE_PRIVATE);
fos.write("Alex".getBytes());
fos.close();

Here's how to read that file:

FileInputStream fis = openFileInput("urls.txt");
int c;
while((c=fis.read())!=-1)
{

k += (char)c;
}
fis.close();

String k will contain "Ankit" as a string.

Mind you.. the file "urls.txt" gets formed in the phone memory, you cannot access that file in your project as a resource.

For more information see: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

Write/Read any object to/from Android memory

You wrote :

public void readObjectFromMemory(String filename, Object object) {
//...
object = is.readObject();

But object is only a copy of the argument you passed to the function. You can change that copy inside the method (as you do), but this will have no effect on the actual parameter.

You have to return your object instead :

public Object readObjectFromMemory(String filename) {
FileInputStream fis;
Object obj;
try {
fis = game.openFileInput(filename);
ObjectInputStream is = new ObjectInputStream(fis);
obj = is.readObject();
is.close();
}
catch (...) {
return null;
}

return obj;
}

Read this for more details : http://www.cs.utoronto.ca/~dianeh/tutorials/params/

How do I read the file content from the Internal storage - Android App

Take a look this how to use storages in android http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

To read data from internal storage you need your app files folder and read content from here

String yourFilePath = context.getFilesDir() + "/" + "hello.txt";
File yourFile = new File( yourFilePath );

Also you can use this approach

FileInputStream fis = context.openFileInput("hello.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}


Related Topics



Leave a reply



Submit