Let's take a look at an example first
Import java.io.*;
/**
* Created by liguoqing on 2016/3/28.
*/Public
class Readtxtfile {public
static void Readtxt (String filePath) {
try {
file File = new file (fil Epath);
if (File.isfile () && file.exists ()) {
InputStreamReader ISR = new InputStreamReader (New FileInputStream ( File), "Utf-8");
BufferedReader br = new BufferedReader (ISR);
String linetxt = null;
while ((Linetxt = Br.readline ())!= null) {
System.out.println (linetxt);
}
Br.close ();
} else {
System.out.println ("file does not exist!");
}
catch (Exception e) {
System.out.println ("File read error!")
}
public static void Main (string[] args) {
String FilePath = "d:\\test\\ me. txt";
Readtxt (FilePath);
}
After reading the example above, we will examine the following details
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 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 {Strin
G 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);
}
}