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