可以通过字节流(FileInputStream)、字符流(InputStreamReader)、字符缓冲流(BufferedReader)读取文本中的数据。
1、FileInputStream读取文本
public void read(){
String path = "fileTest.txt";
FileInputStream in = null;
try {
in = new FileInputStream(path);
byte[] temp = new byte[10] ;
int len;
StringBuffer sb = new StringBuffer();
// 文件中有中文时会乱码
while ((len=in.read(temp))!=-1){
String s = new String(temp, 0, len);
sb.append(s);
System.out.println("--------[-" + String.valueOf(s) + "-]----");
}
System.out.println();
System.out.println(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(in!= null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
运行说明:
1)出现乱码(文本是GBK的,程序默认是utf-8)
2)按照设置的字符长度,一段段的输出(据说实际上是一个个字节的读取,效率比较低)
其他:
字节流可以处理所有的数据类型,例如图片、视频、文本
InputStreamReader读取文本
public void read2(){
String path = "fileTest.txt";
FileInputStream in = null;
InputStreamReader inputStreamReader = null;
try {
in = new FileInputStream(path); // 字节流
inputStreamReader = new InputStreamReader(in, "GBK"); // 字符流
char[] temp = new char[20];
int len;
StringBuffer sb = new StringBuffer();
while ((len = inputStreamReader.read(temp)) != -1){
String s = new String(temp, 0, len);
sb.append(s);
System.out.println("--------[-" + String.valueOf(s) + "-]----");
}
System.out.println();
System.out.println(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(in!= null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inputStreamReader != null){
try {
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
运行说明:
1)InputStreamReader可以设置编码格式,读取中文时可以正常显示
2)按照设置的字符长度,一段段的输出(据说实际上是一个个字符的读取)
其他:
字符流只能处理文本数据
BufferedReader读取文本
public void read3(){
String path = "fileTest.txt";
FileInputStream in = null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
try {
in = new FileInputStream(path); // 字节流
inputStreamReader = new InputStreamReader(in, "GBK"); // 字符流
reader = new BufferedReader(inputStreamReader); // 字符缓冲流
StringBuffer sb = new StringBuffer();
char[] temp = new char[20];
int len;
while ((len = reader.read(temp)) != -1){
String s = new String(temp, 0, len);
sb.append(s);
System.out.println("--------[-" + String.valueOf(s) + "-]----");
}
System.out.println();
System.out.println(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(in!= null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inputStreamReader != null){
try {
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
运行说明:
1)BufferedReader包裹了InputStreamReader,所以中文也能正常显示
2)从打印的结果看,跟InputStreamReader的效果一样(据说BufferedReader更快)
3)reader创建公式
xxInputStream in = ....
InputStreamReader iReader = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(iReader);
BufferedReader读取文本,按行读取
...
StringBuffer sb = new StringBuffer();
String temp;
while ((temp = reader.readLine()) != null){
sb.append(temp);
System.out.println("--------[-" + temp + "-]----");
}
System.out.println();
System.out.println(sb.toString());
...
运行说明:
1)一行行的读取,读取的数据中不包含\n、\r
2)据说没有数据时会断,在数据流异常或者断开时才会返回null
读取文本异常
char[] temp = new char[20];
int len;
StringBuffer sb = new StringBuffer();
while ((len = inputStreamReader.read(temp)) != -1){
sb.append(temp);
System.out.println("-len=[" + len + "]-[-" + String.valueOf(temp) + "-]----");
}
System.out.println();
System.out.println(sb.toString());
运行说明:
1)最后一次读取的时候,残留了上一次的数据。观看源码,read的时候,是一个个char修改的,当i到16的时候,已经没有数据,则返回结果,此时17到19这三个char没有被处理,所以保存数据的时候,要用new String(temp, 0, len)截取数据