java读文件

以下有两种方式读文件

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();
}
原文地址:https://www.cnblogs.com/kwzblog/p/12692490.html