Tool class for storing/reading characters in memory or SD card using Io

Source: Internet
Author: User

Filemanager

Package COM. kale. utils; import Java. io. bufferedreader; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import Java. io. outputstream; import Java. io. printstream; import android. content. context; import android. OS. environment;/*** @ Author: Jack Tony * @ Tips: * <! -- Read and write permissions on the SD card --> <uses-Permission Android: Name = "android. permission. read_external_storage "/> <uses-Permission Android: Name =" android. permission. write_external_storage "/> * @ Date: 2014-6-30 */public class filemanager {private string sd_path; private string filename; private string dirname; Public String getsd_path () {return sd_path ;} /*** @ Param permission: whether the permission is added */Public filemanager (Boolean PE Rmission) {// obtain the directory sd_path = environment of the current external storage device. getexternalstoragedirectory () + "/";}/*** create a file on the SD card ** @ throws ioexception */Public file creatsdfile (string dirname, string filename) throws ioexception {This. filename = filename; this. dirname = dirname; file = new file (creatsddir (dirname), filename); Return file;} Public String getfilewholepath () throws ioexception {If (dirname = NULL | Filename = NULL) {return sd_path + dirname + "/" + filename;} else {Throw new ioexception ("can't find dirname or FILENAME ");}} /*** create a directory on the SD card ** @ Param dirname */Public file creatsddir (string dirname) {file dir = new file (sd_path + dirname); Dir. mkdir (); Return dir;}/*** determines whether the folder on the SD card exists * kale,file.txt-> Kale/file.txt */Public Boolean isfileexist (string dirname, string filename) {f Ile file = new file (sd_path + dirname + "/" + filename); Return file. exists ();}/*** write data in an inputstream to the SD card * example: kale,file.txt, inputstream-> Kale/file.txt * inputstream is = new bytearrayinputstream ("Kale data ". getbytes (); manager. savetosdcard ("dir02", "kale.txt", is); */Public file savetosdcard (string dirname, string filename, inputstream input) {file = NULL; outputstream output = N Ull; try {// create the directory creatsddir (dirname); // create a file = creatsdfile (dirname, filename); Output = new fileoutputstream (File ); byte buffer [] = new byte [5*1024]; // int COUNT = 0; while (input. read (buffer ))! =-1) {// string STR = new string (buffer, 0, count); // STR = new string (Str. getbytes ("iso-8859-1"), "UTF-8");/* system. out. println ("--------- File Manager ---- start --------"); system. out. println (STR); system. out. println ("--------- File Manager ----- end -------"); */output. write (buffer);} output. flush ();} catch (exception e) {e. printstacktrace ();} finally {try {output. close ();} catch (exception e) {e. print Stacktrace () ;}} return file ;}/ *** @ Param dirname * @ Param filename * @ return refers to the characters that read files from the SD card, returns string * @ throws exception */Public String readfromsdcard (string dirname, string filename) throws filenotfoundexception {try {file = new file (sd_path + dirname + "/" + filename ); fileinputstream FCM = new fileinputstream (File); bufferedreader BR = new bufferedreader (New inputstreamreader (ISI); stri Ngbuilder sb = new stringbuilder (""); string line = NULL; // read the file content cyclically while (line = Br. Readline ())! = NULL) {sb. append (line);} BR. close (); return sb. tostring (). trim ();} catch (filenotfoundexception e) {e. printstacktrace (); throw new filenotfoundexception ("No such file");} catch (exception e) {e. printstacktrace ();} return NULL ;} ////////////////////// directly read the files in the package name folder, instead of writing the string to data/ * example: write(file.txt, data) */public static void write (context mcontext, string filename, string Str) {try {/*** mode_private: the file can only be read and written by the current program mode_append: open the file in append mode. The newly written content is appended to the end * mode_world_readable: The file content can be read by other programs * mode_world_writeable: the file content can be read and written by other programs */fileoutputstream Fos = mcontext. openfileoutput (filename, context. mode_private | context. mode_append); printstream PS = new printstream (FOS); // output file content ps. println (STR); PS. close ();} catch (exception e) {e. printstacktrace () ;}}/*** use the file name to read the file * example: Read ("file.txt") under data/package name/Files ") * @ return * returns NULL, indicating that the file cannot be found or an exception */public static string read (context mcontext, string filename) {try {// open the file input stream fileinputstream FCM = mcontext. openfileinput (filename); byte [] buff = new byte [1024]; int hasread = 0; stringbuilder sb = new stringbuilder (""); // read the object content while (hasread = Fi. read (buff)> 0) {sb. append (new string (buff, 0, hasread);} // close the input stream of the file. close (); return sb. tostring ();} catch (filenotfoundexception e) {e. printstacktrace ();} catch (exception e) {e. printstacktrace ();} return NULL ;}}

 

Tool class for storing/reading characters in memory or SD card using Io

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.