Lecture d'un fichier interne
Hello
dans mon appli, j'ai un edittext pour écrire dans un fichier et un textview pour afficher le contenu du fichier, l'écriture fonctionne mais pas la lecture
donc, je voudrais lire un fichier sauvegardé en interne mais j'ai une erreur
au début, je pensais que c'était parce que le fichier n'existait pas mais j'ai toujours le problème après l'avoir créé (je n'ai pas vérifié s'il a bien été créé mais d'après un test précédent, il devrait l'être)
je précise que au 1er lancement de l'application, le fichier n'existe pas et dans ce cas là, ça se contente d'afficher "pas de données"
comme le fichier n'existe pas au début, j'avais utilisé "file.exists()" mais même avec ça, ça plante alors je l'ai mis en commentaires
pour faire simple et court, c'est censé faire ça
Code:
1 2 3 4 5 6 7 8
| si le fichier existe
ça lit son contenu
si y a kkch dans le contenu
ça affiche le contenu
sinon
ça affiche pas de données
sinon
ça affiche pas de données |
merci
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| public String ReadProfile(String filename_profile) {
FileInputStream fIn = null;
InputStreamReader isr = null;
Context context = getBaseContext();
//Context context = null;
char[] inputBuffer = new char[255];
String data = null;
try {
//File file = context.getFileStreamPath(FILENAME + filename_profile);
//if(file.exists()) {
fIn = context.openFileInput(FILENAME + filename_profile);
isr = new InputStreamReader(fIn);
isr.read(inputBuffer);
data = new String(inputBuffer);
return data;
/*}
else {
Toast.makeText(context, R.string.profile_read_n, Toast.LENGTH_SHORT).show();
return getString(R.string.profile_unknown);
}*/
}
catch(Exception e) {
Toast.makeText(context, R.string.profile_read_n, Toast.LENGTH_SHORT).show();
return getString(R.string.profile_unknown);
}
finally {
try {
isr.close();
fIn.close();
}
catch(IOException e) {
Toast.makeText(context, R.string.profile_read_n, Toast.LENGTH_SHORT).show();
return getString(R.string.profile_unknown);
}
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| 08-28 16:43:38.466: ERROR/AndroidRuntime(1283): Uncaught handler: thread main exiting due to uncaught exception
08-28 16:43:38.475: ERROR/AndroidRuntime(1283): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.ice/com.android.ice.Profile}: java.lang.NullPointerException
08-28 16:43:38.475: ERROR/AndroidRuntime(1283): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
08-28 16:43:38.475: ERROR/AndroidRuntime(1283): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
08-28 16:43:38.475: ERROR/AndroidRuntime(1283): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-28 16:43:38.475: ERROR/AndroidRuntime(1283): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-28 16:43:38.475: ERROR/AndroidRuntime(1283): at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 16:43:38.475: ERROR/AndroidRuntime(1283): at android.os.Looper.loop(Looper.java:123)
08-28 16:43:38.475: ERROR/AndroidRuntime(1283): at android.app.ActivityThread.main(ActivityThread.java:4363)
08-28 16:43:38.475: ERROR/AndroidRuntime(1283): at java.lang.reflect.Method.invokeNative(Native Method)
08-28 16:43:38.475: ERROR/AndroidRuntime(1283): at java.lang.reflect.Method.invoke(Method.java:521)
08-28 16:43:38.475: ERROR/AndroidRuntime(1283): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-28 16:43:38.475: ERROR/AndroidRuntime(1283): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-28 16:43:38.475: ERROR/AndroidRuntime(1283): at dalvik.system.NativeStart.main(Native Method)
08-28 16:43:38.475: ERROR/AndroidRuntime(1283): Caused by: java.lang.NullPointerException
08-28 16:43:38.475: ERROR/AndroidRuntime(1283): at com.android.ice.Profile.ReadProfile(Profile.java:412)
08-28 16:43:38.475: ERROR/AndroidRuntime(1283): at com.android.ice.Profile.onCreate(Profile.java:86)
08-28 16:43:38.475: ERROR/AndroidRuntime(1283): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-28 16:43:38.475: ERROR/AndroidRuntime(1283): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
08-28 16:43:38.475: ERROR/AndroidRuntime(1283): ... 11 more |