Simple Java Read File sample sharing _java

Source: Internet
Author: User
Tags readline

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. And then you can start calling.

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.

Copy Code code as follows:

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;

public class H20121012 {
/**
* Function: Java read TXT file content
* Step: 1: Get the file handle first
* 2: Get file handle as input a byte stream, need to read this input stream
* 3: After reading to the input stream, need to read 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 ()) {//Determine if the file exists
InputStreamReader read = new InputStreamReader (
New FileInputStream (file), encoding);//take into account 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 ("The specified file not found");
}
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);
}



}

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.