如何读取文本文件中的内容

如下2种方式实现

1、使用字符流

String lineTxt = null;
List<Integer> list=null;
try{
String encoding="GBK";
File file=new File("F:/11.txt");
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
list=new ArrayList<Integer>();
while((lineTxt = bufferedReader.readLine()) != null){
list.add(Integer.parseInt(lineTxt));
}
read.close();
}
}
}
txt文件里面是数字
1234
1256
2345
等等

2、使用字节流

@Test
public void testInput()throws Exception{
InputStream is=new FileInputStream(path);//读取的文件路径(txt文件)
byte []b=new byte[200];
String str=null;//如果是txt文件,那么就用字符串接收
int lg=0;
while((lg=is.read(b)<0)){
str=new String(b,0,lg);
}
System.out.print(str);//控制台打印读到的内容;
is.close();
}