Read and Write Android TXT files

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.