Package com. wirelessqa. helper;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import java. io. InputStream;
Import org. apache. http. util. EncodingUtils;
Import android. app. Activity;
Public class FileAccess extends Activity {
/**
* 1. File Access in a private folder (/data/package name/files)
*
* @ Param fileName
* @ Param message
*/
Public void writeFileData (String fileName, String message ){
Try {
FileOutputStream fout = openFileOutput (fileName, MODE_PRIVATE );
Byte [] bytes = message. getBytes ();
Fout. write (bytes );
Fout. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
/**
* // Read the file under./data/package name/files/
*
* @ Param fileName
* @ Return
*/
Public String readFileData (String fileName ){
String res = "";
Try {
FileInputStream fin = openFileInput (fileName );
Int length = fin. available ();
Byte [] buffer = new byte [length];
Fin. read (buffer );
Res = EncodingUtils. getString (buffer, "UTF-8 ");
Fin. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
Return res;
}
/**
* Write and read the files in the sdcard directory. Use FileOutputStream instead of openFileOutput.
* Difference: openFileOutput is compiled in raw, and FileOutputStream can be used for any file.
* @ Param fileName
* @ Param message
*/
// File written in the/mnt/sdcard/directory
Public void writeFileSdcard (String fileName, String message ){
Try {
// FileOutputStream fout = openFileOutput (fileName, MODE_PRIVATE );
FileOutputStream fout = new FileOutputStream (fileName );
Byte [] bytes = message. getBytes ();
Fout. write (bytes );
Fout. close ();
}
Catch (Exception e ){
E. printStackTrace ();
}
}
// Read the files in the/mnt/sdcard/directory
Public String readFileSdcard (String fileName ){
String res = "";
Try {
FileInputStream fin = new FileInputStream (fileName );
Int length = fin. available ();
Byte [] buffer = new byte [length];
Fin. read (buffer );
Res = EncodingUtils. getString (buffer, "UTF-8 ");
Fin. close ();
}
Catch (Exception e ){
E. printStackTrace ();
}
Return res;
}
/**
* 2. Get the file from the raw folder in the resource and read the data (the resource file can only be read but cannot be written)
*
* @ Param fileInRaw
* @ Return
*/
Public String readFromRaw (int fileInRaw ){
String res = "";
Try {
InputStream in = getResources (). openRawResource (fileInRaw );
Int length = in. available ();
Byte [] buffer = new byte [length];
In. read (buffer );
Res = EncodingUtils. getString (buffer, "GBK ");
// Res = new String (buffer, "GBK ");
In. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
Return res;
}
/**
* 3. Get files from the asset and read data (resource files can only be read but cannot be written)
*
* @ Param fileName
* @ Return
*/
Public String readFromAsset (String fileName ){
String res = "";
Try {
InputStream in = getResources (). getAssets (). open (fileName );
Int length = in. available ();
Byte [] buffer = new byte [length];
In. read (buffer );
Res = EncodingUtils. getString (buffer, "UTF-8 ");
} Catch (Exception e ){
E. printStackTrace ();
}
Return res;
}
}
Author: wirelessqa