Java Get TXT file content

Source: Internet
Author: User

JAVA Reads TXT file contents

In general, we can read the contents of the TXT file directly through the file stream, but sometimes it may appear garbled! At this point, just set the file character encoding.

  

1 ImportJava.io.BufferedReader;2 ImportJava.io.File;3 ImportJava.io.FileReader;4 5 6  Public classTxttest {7     /**8 * Read the contents of the TXT file9      * @paramfile object to be read by fileTen      * @returnReturn file Contents One      */ A      Public StaticString txt2string (file file) { -String result = ""; -         Try{ theBufferedReader br =NewBufferedReader (NewFileReader (file));//constructs a BufferedReader class to read the file -String s =NULL; -              while((s = br.readline ())! =NULL){//use the ReadLine method to read one line at a time -result = result + "\ n" +s; +             } - Br.close ();  +}Catch(Exception e) { A e.printstacktrace (); at         } -         returnresult; -     } -      -      Public Static voidMain (string[] args) { -File File =NewFile ("D:/lucenedata/test1.txt"); in System.out.println (txt2string (file)); -     } to}
Read file effects:

Java Get TXT file content

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.