Package com. ppmeet;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import org. apache. http. util. EncodingUtils;
Import android. content. Context;
/**
* Class name: FileService <BR>
* Class description: reads android files. <BR>
* PS: <BR>
*
* @ Version 1.00 2010/10/21
* @ Author CODYY) peijiangping
*/
Public class FileService {
Private Context context;
Public FileService (Context c ){
This. context = c;
}
// Read the files in sd
Public String readSDCardFile (String path) throws IOException {
File file = new File (path );
FileInputStream FCM = new FileInputStream (file );
String result = streamRead (FCM );
Return result;
}
// Create a raw resource folder under the res directory. The files here can only be read and cannot be written...
Public String readRawFile (int fileId) throws IOException {
// Obtain the input stream
InputStream is = context. getResources (). openRawResource (fileId );
String result = streamRead (is); // returns a String.
Return result;
}
Private String streamRead (InputStream is) throws IOException {
Int buffersize = is. available (); // get the byte length of the input stream
Byte buffer [] = new byte [buffersize];
Is. read (buffer); // reads data into an array
Is. close (); // close the stream after reading.
String result = EncodingUtils. getString (buffer, "UTF-8"); // sets the obtained data encoding to prevent garbled characters
Return result;
}
// Files in the assets folder can only be read and cannot be written.
Public String readAssetsFile (String filename) throws IOException {
// Obtain the input stream
InputStream is = context. getResources (). getAssets (). open (filename );
String result = streamRead (is); // returns a String.
Return result;
}
// Write a file to the SD card
Public void writeSDCardFile (String path, byte [] buffer) throws IOException {
File file = new File (path );
FileOutputStream fos = new FileOutputStream (file );
Fos. write (buffer); // write the buffer array. To write some simple characters, you can write String. getBytes () to the file;
Fos. close ();
}
// Write the file to the files directory of the application's data/data
Public void writeDateFile (String fileName, byte [] buffer) throws Exception {
Byte [] buf = fileName. getBytes ("iso8859-1 ");
FileName = new String (buf, "UTF-8 ");
// Context. MODE_PRIVATE: the default operation mode. This mode indicates that the file is private data and can only be accessed by the application itself. In this mode, the written content will overwrite the content of the original file, if you want to append the newly written content to the original file. You can use Context. MODE_APPEND.
// Context. MODE_APPEND: the mode checks whether the file exists and appends the content to the file if it exists. Otherwise, a new file is created.
// Context. MODE_WORLD_READABLE and Context. MODE_WORLD_WRITEABLE are used to control whether other applications have the permission to read and write the file.
// MODE_WORLD_READABLE: indicates that the current file can be read by other applications; MODE_WORLD_WRITEABLE: indicates that the current file can be written by other applications.
// If you want the file to be read and written by another application, you can input the following:
// OpenFileOutput ("output.txt", Context. MODE_WORLD_READABLE +
// Context. MODE_WORLD_WRITEABLE );
FileOutputStream fos = context. openFileOutput (fileName,
Context. MODE_APPEND); // Add it to the end of the file
Fos. write (buffer );
Fos. close ();
}
// Read the file data in the files directory of the application's data/data
Public String readDateFile (String fileName) throws Exception {
FileInputStream FCM = context. openFileInput (fileName );
String result = streamRead (FCM); // returns a String.
Return result;
}
}
Here is an encapsulation class I have written.
The explanations are all in the annotations.
Android learning and sharing from zookeeper