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.