首页 > 编程语言 > 详细

java读文件

时间:2020-04-13 17:24:57      阅读:61      评论:0      收藏:0      [点我收藏+]

以下有两种方式读文件

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();
}

 

java读文件

原文:https://www.cnblogs.com/kwzblog/p/12692490.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!