Java Read TXT file

Source: Internet
Author: User

Today online random stroll around, somewhere to see the operation of the document, the description is good, deepened the understanding of the operation of the document, in this share to everyone. Hope to have something to gain.

Java reads the contents of the TXT file.

Can be understood as follows:

1. First get a file handle. File File = new file (); Files are file handles, which act as connections. Like, the two people have a telephone network, and then you can start to call.

2. Use this line to read the information of party A: new FileInputStream (file) reads this information into memory. Then party B will need to interpret what he understands.

3. Since you have used FileInputStream (). Then the corresponding need to use InputStreamReader () This method to interpret the memory just loaded in the data

4. Interpret the output after completion. It is natural to convert the data that can be recognized by IO. You can now call the ReadLine () method of BufferedReader () to read each row of data in the TXT file.

/** * @ Author WHS * @ creation Date January 28, 2015 * @ Version V 1.0 */package File.text;import java.io.bufferedreader;import java.io.file;import Java.io.fileinputstream;import java.io.inputstreamreader;/** * Function: Java reads the contents of the TXT file * Step: * 1: Get the file handle first * 2: Get the file handle as the input of a byte code Stream, this input stream needs to be read * 3: After reading to the input stream, you need to read the output of the generated byte stream * 4: one row. ReadLine (). * Note: The exception needs to be considered * @param filePath */public class Txtfile {public static void FileText (string filePath) {try {string Encodin g = "GBK"; File File = new file (filePath);//Create a document handle if (File.isfile () && file.exists ()) {//Determine if the file exists//loaded into memory, and consider the encoding format when reading InputStreamReader read = new InputStreamReader (new FileInputStream (file), encoding); BufferedReader BufferedReader = new BufferedReader (read); String linetxt = Null;while ((linetxt = Bufferedreader.readline ()) = null) {System.out.println (linetxt);} Read.close ();} else {System.out.println ("The specified file cannot be found");}} catch (Exception e) {System.out.println ("Error reading file contents"); E.printstacktrace ();}} public static void Main (string[] args) {String FilePath = "d:\\20121012.txt "; FileText (FilePath);}} 

Java Read TXT 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.