import
java.io.*;
/**
* Created by liguoqing on 2016/3/28.
*/
public
class
ReadTxtFile {
public
static
void
readTxt(String filePath) {
try
{
File file =
new
File(filePath);
// String encoding=
"GBK"
;
if
(file.isFile() && file.exists()) {
InputStreamReader isr =
new
InputStreamReader(
new
FileInputStream(file)
);
//InputStreamReader isr =
new
InputStreamReader(
new
FileInputStream(file), encoding
);
BufferedReader br =
new
BufferedReader(isr);
String lineTxt =
null
;
while
((lineTxt = br.readLine()) !=
null
) {
System.out.println(lineTxt);
}
br.close();
}
else
{
System.out.println(
"文件不存在!"
);
}
}
catch
(Exception e) {
System.out.println(
"文件读取错误!"
);
}
}
public
static
void
main(String[] args) {
String filePath =
"D:\\test\\文件读取测试.txt"
;
readTxt(filePath);
}
首先获得一个文件句柄。File file = new File(); file即为文件句柄。两人之间连通电话网络了。接下来可以开始打电话了。
通过这条线路读取甲方的信息:new FileInputStream(file) 目前这个信息已经读进来内存当中了。接下来需要解读成乙方可以理解的东西
既然你使用了FileInputStream()。那么对应的需要使用InputStreamReader()这个方法进行解读刚才装进来内存当中的数据
解读完成后要输出呀。那当然要转换成IO可以识别的数据呀。那就需要调用字节码读取的方法BufferedReader()。同时使用bufferedReader()的readline()方法以读取每一行的的形式读取txt文件中的数据。