Java Read file

Source: Internet
Author: User

Used to Python in Java, read the file this really feel trouble, wrote a small piece of code on the Java read and write a simple package, finally comfortable a bit:

 Packageseleniumtest;Importjava.io.IOException;ImportJava.io.File;ImportJava.io.FileReader;ImportJava.io.FileWriter;ImportJava.io.BufferedReader;Importjava.util.List;Importjava.util.ArrayList; Public classMyFile { PublicFile F;  PublicFileWriter FW;  PublicFileReader fr; MyFile (String path) {f=NewFile (path); if(F.exists ()! =true){            Try{f.createnewfile (); }Catch(Exception e) {e.printstacktrace (); }        }         This. Createwriter ();  This. Createreader (); }        Private voidCreatewriter () {Try{FW=NewFileWriter (F,true); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }        Private voidCreatereader () {Try{FR=NewFileReader (f); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }         PublicFileWriter getwriter () {returnFW; }         PublicBufferedReader Getreader () {bufferedreader br=NewBufferedReader (FR); returnBR; }         Public Static voidAppendwriter (String path, list<string>lines) {MyFile tmp=NewMyFile (path);        Tmp.createwriter (); FileWriter FW=Tmp.getwriter ();  for(String line:lines) {Try{fw.write (String.Format ("%s%s", Line.trim (), "\ r \ n")); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }        Try{fw.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }         Public StaticList<string>myreader (String path) {List<String> result =NewArraylist<string>(); MyFile tmp=NewMyFile (path);        Tmp.createwriter (); BufferedReader FR=Tmp.getreader ();        String s; Try {             while((s = fr.readline ())! =NULL) {Result.add (s); }        } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        returnresult; }}
View Code

Java Read file

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.