Creation of the Textfile class

Source: Internet
Author: User
Tags set set string splitter

Textfile, as a self-written method, inherits from List<string>. Like what words appear in the statistics text, do not repeat, etc., it is appropriate to use set set to complete the statistics.

1 classTextfileextendsArraylist<string>{2          Public Staticstring Read (string filename) {3StringBuilder sb=NewStringBuilder ();4             Try{5BufferedReader in=NewBufferedReader (NewFileReader (6                         NewFile (filename). Getabsolutefile ());7                     Try{8 String S;9                          while((S=in.readline ())! =NULL){Ten Sb.append (s); OneSb.append ("\ n"); A                             } -}finally{ - in.close (); the                 } -}Catch(IOException e) { -                 Throw NewRuntimeException (e); -             } +             returnsb.tostring (); -     } +      Publictextfile (String filename,string splitter) { A         Super(arrays.aslist (read (filename). Split (splitter))); at         if(Get (0). Equals ("")) Remove (0); -     } -      Publictextfile (String filename) { -          This(filename, "\ n"); -     } -      Public voidWrite (String filename) { in         Try{ -PrintWriter out=NewPrintWriter (NewFile to (filename). Getabsolutefile ()); +             Try{ -                  for(String item: This) out.println (item); the}finally{ * out.close (); $                 }Panax Notoginseng}Catch(IOException e) { -                 Throw NewRuntimeException (e); the                 } +              A     } the      Public Static voidWrite (String filename,string text) { +     //where filename indicates the name of the file to write, text indicates the content of the string being written -         Try{ $FileWriter fwriter=NewFileWriter (NewFile $ (filename). Getabsolutefile ()); -BufferedWriter out=NewBufferedWriter (fwriter); -String []tx=text.split ("\ n"); the             Try{ -                  for(inti=0;i<tx.length;i++)Wuyi                 { the Out.write (Tx[i]); - out.newline (); Wu                 } -}finally{ About Out.flush (); $ out.close (); -                 } -}Catch(IOException e) { -                 Throw NewRuntimeException (e); A                  +             } the              -     } $}
     Public Static void Main (string[] args) {        Setnew treeset<string>(            new textfile (" Statcktest.java "," \\w+ "));        System.out.print (words);            System.out.println (Words.size ());                SetNew treeset<string>(string.case_insensitive_order);        Words2.addall (words);        System.out.print (WORDS2);        System.err.println (Words2.size ());                 // What's the difference here?             }

Creation of the Textfile class

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.