Java Read txt file method _java

Source: Internet
Author: User
Tags readline

Java reads TXT file contents. You can make the following understanding:

    • First get a file handle. File File = new file (); File is the document handle. There is a telephone network between the two people. Then you can start the phone call.
    • Read party A's message through this line: New FileInputStream (file) This information is now read in memory. Then we need to interpret what party B can understand.
    • Since you have used FileInputStream (). Then the corresponding need to use the InputStreamReader () method to interpret the data just loaded in memory
    • After the completion of the interpretation to output AH. Of course it's going to be converted into IO-identifiable data. Then you need to invoke the byte code read Method BufferedReader (). Use the BufferedReader () ReadLine () method to read each row of data in the TXT file ha.
Package Com.campu;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.InputStreamReader;
 
Import Java.io.Reader; /** * @author code farmer Xiao H20121012.java * 2012-10-12 pm 11:40:21/public class H20121012 {/** * function: Java read TXT file Content * Step: 1: First get file handle * 2: Get file handle as input a byte stream, you need to read the input stream * 3: After reading to the input stream, you need to read the generated byte stream * 4: One line of output.
   ReadLine ().
 * Note: Exceptions need to be considered * @param filePath/public static void Readtxtfile (string filePath) {try {string
        Encoding= "GBK";
        File File=new file (FilePath);
          if (File.isfile () && file.exists ()) {//To determine if the file exists inputstreamreader read = new InputStreamReader (
          New FileInputStream (file), encoding)//Considering the encoding format BufferedReader BufferedReader = new BufferedReader (read);
          String linetxt = null; while ((Linetxt = Bufferedreader.readline ())!= null) {System.out.println (LinEtxt);
    } read.close ();
    }else{System.out.println ("Cannot find the specified file");
      The catch (Exception e) {System.out.println ("Error reading file contents");
    E.printstacktrace (); } public static void Main (string argv[]) {string filePath = "L:\\apache\\htdocs\\res\\20121012.txt";
    /"res/";
  Readtxtfile (FilePath);

 }  
 
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.