Java 无法读取刚写入的文件
我正在尝试从写入了一些用户凭据的文件中读取。我想将文件写入内部存储位置。守则:Java 无法读取刚写入的文件,java,android,file-io,Java,Android,File Io,我正在尝试从写入了一些用户凭据的文件中读取。我想将文件写入内部存储位置。守则: private void writeSendDetails(String name, String number, String emailID) { //This function writes details to userCredentials.txt and also sends it to server. String text = "Name: " + userName
private void writeSendDetails(String name, String number, String emailID) {
//This function writes details to userCredentials.txt and also sends it to server.
String text = "Name: " + userName + "\n" + "Number: " + userNumber + "\n" + "Email ID:" + userEmailID;
FileOutputStream fos = null;
try {
fos = openFileOutput(userCredFile, MODE_PRIVATE);
Log.v(this.toString(), fos.toString());
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(fos != null) {
OutputStreamWriter osw = new OutputStreamWriter(fos);
try {
osw.write(text);
} catch (IOException e) {
// TODO Auto-generated catch block
Log.v(this.toString(), "IOException caught in osw.write");
e.printStackTrace();
}
try {
osw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
osw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Log.v(this.toString(), "Written everything to userCredentials.txt");
readUserCredentials();
//code to send to server should begin here.
}
private void readUserCredentials() {
//function to read name, number and email ID from userCredentials.txt
/* Steps:
* 1. Check if file exists.
* 2. If it does, read all relevant credentials.
*/
File f = new File(userCredFile);
if(f.canRead()) {
Log.v(this.toString(), "Can open userCredentials for reading from.");
}
try {
FileReader fis = new FileReader(f);
Log.v(this.toString(), "Wrapping a buffered reader around file reader.");
BufferedReader bufRead = new BufferedReader(fis, 100);
String line;
try {
while((line = bufRead.readLine()) != null) {
Log.v(this.toString(), "Line read = " + line);
line = bufRead.readLine();
if(line.indexOf("Name: ") != -1) {
Log.v(this.toString(), "Found name in the string.");
userName = line.substring(6);
} else if(line.indexOf("Number: ") != -1) {
Log.v(this.toString(), "Found number in the string.");
userNumber = line.substring(8);
} else if(line.indexOf("Email ID: ") != -1) {
Log.v(this.toString(), "Found email in the string.");
userEmailID = line.substring(10);
}
}
Log.v(this.toString(), "User credentials = " + userName + " " + userNumber + " " + userEmailID);
} catch (IOException e) {
Log.v(this.toString(), "IOException caught.");
}
} catch (FileNotFoundException e1) {
Log.v(this.toString(), "File not found for reading.");
}
}
LogCat
输出显示:
04-14 15:20:43.789: V/com.sriram.htmldisplay.htmlDisplay@44c0c660(675): Written everything to userCredentials.txt
04-14 15:20:43.789: V/com.sriram.htmldisplay.htmlDisplay@44c0c660(675): File not found for reading.
04-14 15:20:43.889: V/com.sriram.htmldisplay.fireflymenu@44c401e0(675): File not found for reading.
我的问题:1.我需要将文件写入内部存储器。我做得对吗?
2.为什么刚写的文件没有被读取 代码中的一些内容:
Log.e(标记,“message”,e)
而不是eclipse中自动创建的东西李>
文件NotFoundException
使用上面提到的Log.e()
可以打开用户凭据进行读取。
输出
原因很简单:您可以使用openFileOutput(userCredFile,MODE_PRIVATE)创建文件代码>。如果你读了这本书,你会偶然发现:
要打开的文件的名称;不能包含路径分隔符
这意味着userCredFile
只能是类似于test.txt的内容。此外,该方法在“外部”无法轻松访问的目录中创建文件
当您现在尝试通过FileReader(userCredFile)
读取文件时,很明显,android会尝试在根目录中打开它:/text.txt
,当然,它会失败。没有非根应用程序可以在根目录中写入/读取
主要问题,也是您的问题的答案:为什么不使用相应的openFileInput(userCredFile)
方法来读取文件?您有很多这样的错误:printStackTrace()
,它可能隐藏了实际错误,Log.e()
这些错误来查看文件是否确实被写入。我猜在编写过程中也找不到它。printStackTrace()
是Eclipse生成的自动实现存根的一部分。在编写过程中不会捕获异常。仅仅因为Eclipse生成了异常,并不意味着printStackTrace()
是正确的做法。