FileReader上的FileNotFoundException

时间:2018-05-14 07:18:18

标签: java android filereader

我的应用程序需要选择一个.txt文档,打开它,然后逐行读取。我可以得到文件路径没问题,但FileReader不会打开文件,抛出FileNotFoundException。

我的代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
        case ACTIVITY_CHOOSE_FILE: {
            if (resultCode == RESULT_OK){
                Uri uri = data.getData();
                filePath = uri.getPath();
                if (filePath != ""){
                    txtvFileSelected.setText(filePath);
                    try {
                        // FileReader reads text files in the default encoding.
                        FileReader fileReader = new FileReader(filePath);
                        // Always wrap FileReader in BufferedReader.
                        BufferedReader bufferedReader = null;
                                new BufferedReader(fileReader);
                        int currentLine = 0;
                        while((line = bufferedReader.readLine()) != null) {
                            System.out.println(line);
                            //TODO: add string separation into floats here.
                            currentLine++;
                        }
                        // Always close files.
                        bufferedReader.close();
                    }
                    catch(FileNotFoundException ex) {
                        System.out.println("File not found '" + filePath + "'");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                else
                    txtvFileSelected.setText("No file selected");
            }
        }
    }
}

错误日志:

05-14 09:14:51.409 24212-24279/com.examens.gilian.robotapplication OpenGLRenderer: Initialized EGL, version 1.4
05-14 09:14:53.428 24212-24279/com.examens.gilian.robotapplication D/OpenGLRenderer: endAllActiveAnimators on 0xb8ad5368 (RippleDrawable) with handle 0xb8988670
05-14 09:14:59.046 24212-24212/com.examens.gilian.robotapplication I/System.out: File not found '/document/primary:media/Data.txt'

1 个答案:

答案 0 :(得分:1)

  

filePath = uri.getPath();

这不是一个文件系统路径,所以难怪找不到任何东西。

而是打开输入流并从流中读取。

    function Champion(champName) {
  //CHEERIO webscraping
  var cheerio = require('cheerio');
  //REQUEST http library
  var request = require('request');
  //url of the champion
  var url = "http://champion.gg/champion/Camille/Top?";
  var most_frequent_completed_build;
  var highest_win_percentage_completed_build;
  request(url,
    function(error, response, html) {
      if (!error && response.statusCode == 200) {
        var $ = cheerio.load(html);
        var final_build_items = $(".build-wrapper a");
        var mfcb = [];
        var hwpcb = [];
        for (i = 0; i < 6; i++) {
          var temp = final_build_items.get(i);
          temp = temp.attribs.href;
          //slices <'http://leagueoflegends.wikia.com/wiki/> off the href
          temp = temp.slice(38);
          mfcb.push(temp);
        }
        for (i = 6; i < 12; i++) {
          var temp = final_build_items.get(i);
          temp = temp.attribs.href;
          //slices <'http://leagueoflegends.wikia.com/wiki/> off the href
          temp = temp.slice(38);
          hwpcb.push(temp);
        }
        most_frequent_completed_build = mfcb;
        highest_win_percentage_completed_build = hwpcb;
      } else {
        console.log("Response Error: " + response.statusCode);
      }
    }
  );
  return {most_frequent_completed_build:most_frequent_completed_build};
};
module.exports = Champion;

    var temp = new Champion("hello");
console.log(temp.most_frequent_completed_build);