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);
}
}