내가하고 싶은 것은 간단한 int []를 만들고있는 앱에서 20 개의 최고 기록으로 저장하고 업데이트하는 것입니다.하지만 계속 fileNotFoundExceptions를 얻고 있습니다. 나는 읽었다 : https://developer.android.com/guide/topics/data/data-storage.html#filesExternal, 그러나 그것은 믿을 수 없을만큼 구체적이지 않았다.간단한 int []를 안드로이드 메모리에 작성합니다.
사용 분야 :
private int[] highscores; //initialized as null in onCreate()
private double dynamicHighScore; //calculated from intent that fires activity
코드 : 내가 파일을 얻을 수 없다 후속 실행 후
GameOver시String fileName = "highscores";
try {
FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis); //firing fileNotFoundException everytime
this.highScores = (int[]) ois.readObject();
Log.d("GameOver", "Object Read");
ois.close();
fis.close();
if (this.highScores.length < 20) {
int[] newHighScores = new int[this.highScores.length + 1];
newHighScores[newHighScores.length - 1] = (int) this.dynamicScore;
this.highScores = newHighScores;
} else if (this.highScores[this.highScores.length - 1] < this.dynamicScore) {
this.highScores[this.highScores.length - 1] = (int) this.dynamicScore;
}
int j;
int key;
int i;
// insertion Sort
for (j = 1; j < highScores.length; j++) {
key = highScores[j];
for (i = j - 1; (i >= 0) && (highScores[i] < key); i--) {
highScores[i + 1] = highScores[i];
}
highScores[i + 1] = key;
}
FileOutputStream fos = openFileOutput(fileName,
Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(highScores);
Log.d("GameOver", "Object rewritten");
oos.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.d("fnf", "fnf1");
} catch (IOException e) {
e.printStackTrace();
Log.d("IO", "IO1");
} catch (ClassNotFoundException e) {
e.printStackTrace();
Log.d("CNF", "CNF1");
}
if (this.highScores == null) {
try {
this.highScores = new int[] { (int) this.dynamicScore };
FileOutputStream fos = openFileOutput(fileName,
Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(highScores);
Log.d("GameOver", "Object created and written");
oos.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
가 나는 문자열 파일 이름 대신 파일 개체를 전달하는 시도 를 찾을 수 새로운 FileInputStream()으로 변경했지만 아무런 변화가 없음 My Logcat은 객체가 메모리에 쓰여지고 있음을 나타냅니다. 에서 찾을 수 없습니다.
파일 경로를 명시 적으로 지정해야합니까? 그렇다면 어디에서?
파일 경로를 더 명시 적으로 지정해야하는 경우 내 Logcat이 성공적으로 개체 생성 및 파일 쓰기를 로깅하는 이유는 무엇입니까?
이 int []는 메모리에 저장하고 액세스하려는 유일한 객체입니다. 어떤 도움이 시몬의 의견에 의해 지적 훨씬 더 좋은 방법은 (된 SharedPreferences 같은) 아마이,
왜 SharePreferences를 사용하여 모든 파일 논리를 잃지? – Simon