Android File (Save in Application)

In Android, Application can save file under somewhere.

Maybe after saving, we can use it as setting file or contents.

Application can save….

  1. Application
  2. Storage(Internal, External)

etc…

Let’s explain.

In Application

Application specific directory is under /data/data/<application_package>/files

application_package is application package FQDN.

In real machine, we can’t see /data/data in application directory, though can see in EmulatorCool
If you want to test in application directory, we have no choice to use device.

I recommend we save settings files or something in this area.

Sample

private void write()
{
	BufferedWriter bw = null; 
	try
	{
		FileOutputStream os = openFileOutput("data.txt", MODE_PRIVATE);	// Open file under application dir
		bw = new BufferedWriter(new OutputStreamWriter(os));
		bw.write("Hello Japan!");
	}
	catch ( Exception oops )
	{
		oops.printStackTrace();
	}
	finally
	{
		if ( bw != null )
		{
			try
			{
				bw.close();
			}
			catch ( IOException oops )
			{
				oops.printStackTrace();
			}
		}
	}
}

private void read()
{
	BufferedReader br = null;
	try
	{
		FileInputStream is = openFileInput("data.txt");
		br = new BufferedReader(new InputStreamReader(is));
		String line;
		while( (line = br.readLine()) != null)
		{
			Log.i(TAG, line);
		}
	}
	catch ( Exception oops )
	{
		oops.printStackTrace();
	}
	finally
	{
		if ( br != null )
		{
			try
			{
				br.close();
			}
			catch ( IOException oops )
			{
				oops.printStackTrace();
			}
		}
	}
}

To read and write in here, Android prepared Stream by Context, also can use File object(java.utils.File) Smile

Next is storage,… Please go to next