[Datastructure] A useful util class for reading and writing files

Source: Internet
Author: User

Faced with the upcoming exam, some useful methods referred to file operation drew tremenous attention. Now I make a summary to reading file.

Import Java. io. bufferedreader; import Java. io. bufferedwriter; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. inputstreamreader; import Java. io. outputstreamwriter; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import Java. util. map; public class fileutil {public St Atic final string Sep = ":"; public static final string encoding = "UTF-8";/*** read the content from file, the format of content is like "key: value "* todo ** @ time Jul 17,201 4 11:21:50 am * @ Param filepath * @ return * @ throws exception * @ return Map <string, string> */public static Map <string, string> readfiletomap (string filepath) throws exception {system. out. println ("Read File [" + filepath + "] On Start... "); Map <string, string> contents = new hashmap <string, string> (); file = new file (filepath); bufferedreader reader = NULL; try {reader = new bufferedreader (New inputstreamreader (New fileinputstream (file), encoding); string info = NULL; while (Info = reader. readline ())! = NULL) {contents. put (info. split (SEP) [0], info. split (SEP) [1]) ;}} catch (filenotfoundexception e) {system. out. println ("file [" + filepath + "] Not found. "); throw E;} catch (ioexception e) {system. out. println ("file IO operation error, exception information:" + E. getmessage (); throw E;} catch (exception e) {system. out. println ("file read error, exception information:" + E. getmessage (); throw E;} finally {If (reader! = NULL) {try {reader. close ();} catch (ioexception e) {system. out. println ("An error occurred while disabling the IO stream. Exception information:" + E. getmessage (); throw e ;}} system. out. println ("Read File [" + filepath + "] ends. "); Return contents;}/*** read the content from file, every line will be an element of list. ** @ time Jul 17,201 4 11:39:07 am * @ Param filename * @ return list <string> */public static list <string> readfiletolist (S Tring filename) throws exception {list <string> Infos = new arraylist <string> (); bufferedreader reader = NULL; try {reader = new bufferedreader (New inputstreamreader (New fileinputstream (new file (filename), "UTF-8"); string info = NULL; while (Info = reader. readline ())! = NULL) {Infos. add (Info) ;}} catch (filenotfoundexception e) {system. out. println ("file [" + filename + "] Not found. "); throw E;} catch (ioexception e) {system. out. println ("file IO operation error, exception information:" + E. getmessage (); throw E;} finally {try {If (reader! = NULL) {reader. close () ;}} catch (ioexception e) {e. printstacktrace () ;}} return Infos;}/*** read all the content from filepath at a time. ** @ time Jul 17,201 4 11:45:55 am * @ Param filepathorfilename * @ return string */public static string readfiletostring (string filepath) throws exception {inputstreamreader isreder = NULL; bufferedreader BR = NULL; stringbuffer sb = new strin Gbuffer (); try {file F = new file (filepath); If (F. isfile () & F. exists () {isreder = new inputstreamreader (New fileinputstream (F), "UTF-8"); BR = new bufferedreader (isreder); string line; while (line = BR. readline ())! = NULL) {sb. append (line ). append ("\ r \ n") ;}} catch (exception e) {system. out. println ("exception when reading the file" + filepath); throw E;} finally {If (isreder! = NULL) {try {isreder. Close () ;}catch (ioexception e) {e. getmessage () ;}} if (BR! = NULL) {try {BR. close ();} catch (ioexception e) {e. getmessage () ;}} return sb. tostring ();} /*** Write result to the filename ** @ time Jul 17,201 4 4:57:57 * @ Param result * @ Param filename * @ return void */public static void writefile (string result, string filename) {file F = new file (filename); outputstreamwriter OSW = NULL; bufferedwriter BW = NULL; try {If (! F. exists () {f. createnewfile ();} OSW = new outputstreamwriter (New fileoutputstream (F), "UTF-8"); // "GBK" BW = new bufferedwriter (OSW); BW. write (result);} catch (ioexception e) {system. out. println ("ioexception when writing result," + E. getmessage ();} catch (exception e) {system. out. println ("exception when writing result," + E. getmessage ();} finally {If (BW! = NULL) {try {BW. close ();} catch (ioexception e) {system. out. println ("ioexception when closing filewriter," + E. getmessage () ;}} if (OSW! = NULL) {try {OSW. close ();} catch (ioexception e) {system. out. println ("ioexception when closing bufferwriter," + E. getmessage () ;}}} public static void main (string [] ARGs) throws exception {Map <string, string> testmap = fileutil. readfiletomap ("input.txt"); system. out. println ("---------------------"); For (map. entry <string, string> entry: testmap. entryset () {system. out. println (entry. getkey () + ":" + entry. getvalue ();} system. out. println ("---------------------"); List <string> testlist = fileutil. readfiletolist ("input.txt"); For (string STR: testlist) {system. out. println (STR);} system. out. println ("---------------------"); string test = fileutil. readfiletostring ("input.txt"); fileutil. writefile (test, "output.txt ");}}


 

 

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.