文件存在,但在吐司中它是空的

时间:2017-08-15 10:22:41

标签: java android

toast说该文件存在,但之后读取响应为空。可能是什么错误?

由于

fileName = "locations.txt";
File file = new File(Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/doma/" + fileName);

    String response = "";

    if (file.exists()) {
        Toast.makeText(this, fileName + " exists", Toast.LENGTH_SHORT).show();
        BufferedReader br;

        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "cp1252"));
            for (String line; (line = br.readLine()) != null; response += line + '\n') ;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Toast.makeText(this, "read:\n" + response, Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "Sorry, " + fileName + " doesn't exist!!", Toast.LENGTH_LONG)
                .show();
    }

1 个答案:

答案 0 :(得分:0)

如果您提及回复,则将其设为空Toast.makeText(this, "read:\n" + response, Toast.LENGTH_SHORT).show();

你的toast必须是file而不是fileName ..你在哪里获得fileName的值?

File file = new File(Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/doma/" + fileName);

    String response = "";

    if (file.exists()) {
        Toast.makeText(this, fileName + " exists", Toast.LENGTH_SHORT).show();
        BufferedReader br;

        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "cp1252"));
            for (String line; (line = br.readLine()) != null; response += line + '\n') ;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

Toast.makeText(this, "read:\n" + response, Toast.LENGTH_SHORT).show();
                .show();
    } else {
     Toast.makeText(this, "Sorry, " + file + " doesn't exist!!", Toast.LENGTH_LONG)
    }