Java 文件存在,但在toast-it';它是空的
toast表示文件存在,但在此之后,读取响应为空。可能是什么错误 谢谢Java 文件存在,但在toast-it';它是空的,java,android,Java,Android,toast表示文件存在,但在此之后,读取响应为空。可能是什么错误 谢谢 fileName = "locations.txt"; File file = new File(Environment.getExternalStorageDirectory() .getAbsolutePath() + "/doma/" + fileName); String response = ""; if (file.exists()) { Toast.m
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();
}
如果您引用的响应被设置为空
Toast.makeText(这是“read:\n”+response,Toast.LENGTH_SHORT).show()代码>
您的吐司必须是文件,而不是文件名。。从哪里获得文件名的值
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)
}
如果您引用的响应被设置为空Toast.makeText(这是“read:\n”+response,Toast.LENGTH_SHORT).show()代码>
您的吐司必须是文件,而不是文件名。。从哪里获得文件名的值
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)
}
如果您引用的响应是空的Toast.makeText(这是“read:\n”+response,Toast.LENGTH_SHORT).show()代码>是的,变量响应为空,但不应为空。可能是什么问题?这应该可以工作…文件不是空的,是吗?您的try-catch块中是否提出了一个exeption?好的,问题已经解决了。缺少权限请求。谢谢你的帮助!如果您引用的响应是空的Toast.makeText(这是“read:\n”+response,Toast.LENGTH_SHORT).show()代码>是的,变量响应为空,但不应为空。可能是什么问题?这应该可以工作…文件不是空的,是吗?您的try-catch块中是否提出了一个exeption?好的,问题已经解决了。缺少权限请求。谢谢你的帮助!你是对的,我修复了它,现在仍然没有任何内容,请仔细检查这个(Environment.getExternalStorageDirectory().getAbsolutePath()+“/doma/”+fileName)代码>好的,问题已经解决了。缺少权限请求。谢谢你的帮助!你是对的,我修复了它,现在仍然没有任何内容,请仔细检查这个(Environment.getExternalStorageDirectory().getAbsolutePath()+“/doma/”+fileName)代码>好的,问题已经解决了。缺少权限请求。谢谢你的帮助!