Case study of android development file read write application

  • 2020-05-07 20:26:15
  • OfStack

1. Basic concept
Save the file in the Android application in two locations
The phone comes with a small storage space (such as 200M), suitable for saving 1 small files, Android save location in data/data/ application package name /files directory
External storage devices such as SD card, large, suitable for saving large files such as video, Android save location in mnt/sdcard directory, androd1.5, android1.6 saved in sdcard directory
The location of the save can be found in the file explorer view of android

2. Example
 
/** 
*  File action class  
* 
* @author XY 
* 
*/ 
public class FileService 
{ 
/** 
*  Context object  
*/ 
private Context context; 
public FileService(Context context) 
{ 
super(); 
this.context = context; 
} 
/** 
*  Save files to the phone's own storage space  
* 
* @param filename  The file name  
* @param fileContent  The file content  
*/ 
@SuppressWarnings("static-access") 
public void save(String filename, String fileContent) throws Exception 
{ 
FileOutputStream fos = context.openFileOutput(filename, context.MODE_PRIVATE); //  By default, it is stored in the phone's own storage space  
fos.write(fileContent.getBytes("UTF-8")); 
fos.close(); 
} 
/** 
*  Save the file to SD card  
* 
* @param filename  The file name  
* @param fileContent  The file content  
*/ 
public void saveInSDCard(String filename, String fileContent) throws Exception 
{ 
//  If the file is saved in SDCard , the file is not controlled by read and write  
File file = new File(Environment.getExternalStorageDirectory(), filename); 
FileOutputStream fos = new FileOutputStream(file); 
fos.write(fileContent.getBytes("UTF-8")); 
fos.close(); 
} 
/** 
*  Read file contents  
* 
* @param filename  The file name  
* @return 
*/ 
public String read(String filename) throws Exception 
{ 
FileInputStream fis = context.openFileInput(filename); 
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 
byte[] buffer = new byte[1024]; 
int len = 0; 
//  Read the content to buffer , until the end is -1 
while ((len = fis.read(buffer)) != -1) 
{ 
//  This example will read into a byte array each time (buffer variable ) The content is written to the memory buffer to save the content each time  
outStream.write(buffer, 0, len); 
} 
//  Fetch data held in memory  
byte[] data = outStream.toByteArray(); 
fis.close(); 
String result = new String(data, "UTF-8"); 
return result; 
} 
} 

MainActivity
 
try 
{ 
//  Store in the phone's own storage space  
fs.save(filename, fileContent); 
Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_SHORT).show(); 
//  Stored on external devices SD card  
//  judge SDCARD Whether it exists and whether it can be read or written  
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) 
{ 
fs.saveInSDCard(filename, fileContent); 
Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_SHORT).show(); 
} 
else 
{ 
Toast.makeText(getApplicationContext(), R.string.failsdcard, Toast.LENGTH_SHORT).show(); 
} 
} 
catch (Exception e) 
{ 
Toast.makeText(getApplicationContext(), R.string.fail, Toast.LENGTH_SHORT).show(); 
Log.e(tag, e.getMessage()); 
} 

File name with no path, direct input such as xy.txt
For SD card operations, you need to add permissions at AndroidManifest.xml
 
<!--  in SDCard Permission to create and delete files  --> 
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> 
<!--  to SDCard Permission to write data in  --> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 


3.1 some API
Environment.getExternalStorageDirectory() to obtain the path of mnt/sdcard directory, for android 1.5,1.6 the path is sdcard directory
Two API are provided in Activity
The path getCacheDir() gets is the data/data/ application package name /cache directory
The path obtained by getFilesDir() is the data/data/ application package name /files directory

Related articles: