일부 사용자 자격 증명이 작성된 파일을 읽으려고합니다. 내부 저장소 위치에 파일을 쓰려고합니다. 코드 :방금 작성한 파일을 읽을 수 없습니다.
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/[email protected](675): Written everything to userCredentials.txt
04-14 15:20:43.789: V/[email protected](675): File not found for reading.
04-14 15:20:43.889: V/[email protected](675): File not found for reading.
내 질문 (들) :
1. 나는 내부 저장 장치에 파일을 작성해야합니다. 내가 제대로하고 있니?
2. 방금 쓴 파일을 읽지 못하는 이유는 무엇입니까? 코드에 대한
당신은 이러한 것들을 많이 가지고 있습니다 :'printStackTrace()'는 실제 오류를 숨길 수 있으며,'Log.e()'는이 오류들로 인해 파일이 실제로 기록되고 있는지를 알 수 있습니다. 내 추측은 쓰기 도중 발견되지 않습니다. – Oren
'printStackTrace()'는 Eclipse가 생성하는 자동 구현 스텁의 일부입니다. – Sriram
쓰기에 예외가 발생하지 않습니다. – Sriram