在ModificaTessera活动中,我将一些数据保存在txt文件中,效果很好。 在MainActivity中我必须读取这些数据,但我不能,因为每次我尝试打开txt文件时都会收到此错误:
java.io.FileNotFoundException:/data/data/appname/files/data.txt:open failed:ENOENT(没有这样的文件或目录)
这是我用来打开和阅读文件的代码:
MainActivity.java
try
{
InputStream inputPosizione = openFileInput("PosizioniTessereUtente.txt");
InputStream inputCodici = openFileInput("CodiciTessereUtente.txt");
if(inputPosizione != null)
{
InputStreamReader streamPosizione = new InputStreamReader(inputPosizione);
InputStreamReader streamCodici = new InputStreamReader(inputCodici);
BufferedReader bufferPosizione = new BufferedReader(streamPosizione);
BufferedReader bufferCodici = new BufferedReader(streamCodici);
Integer pos = null;
String cod = null;
while((pos = Integer.parseInt(bufferPosizione.readLine())) != null)
{
cod = bufferCodici.readLine();
Tessera tmp = new Tessera(negozi.tessereDefault.get(pos).getNome(),
negozi.tessereDefault.get(pos).getLogo());
tmp.setCodice(cod);
tessereUtente.add(tmp);
}
bufferPosizione.close();
bufferCodici.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
在这里,我写TXT文件
ModificaTessera.java
OutputStreamWriter out;
try {
out = new OutputStreamWriter(openFileOutput("PosizioneTessereUtente.txt", MODE_APPEND));
String pos = Integer.toString(posizione);
Log.v("posizione",pos);
out.write(pos);
out.write('\n');
out.close();
Log.v("codice",codice);
out = new OutputStreamWriter(openFileOutput("CodiciTessereUtente.txt", MODE_APPEND));
out.write(codice);
out.write('\n');
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这是LogCat错误:
01-21 19:19:29.529: W/System.err(31592): java.io.FileNotFoundException:
/data/data/com.activity.cardsnuovo/files/PosizioniTessereUtente.txt: open failed: ENOENT (No such file or directory)
01-21 19:19:29.529: W/System.err(31592): at libcore.io.IoBridge.open(IoBridge.java:456)
01-21 19:19:29.529: W/System.err(31592): at java.io.FileInputStream.<init>(FileInputStream.java:76)
01-21 19:19:29.529: W/System.err(31592): at android.app.ContextImpl.openFileInput(ContextImpl.java:955)
01-21 19:19:29.529: W/System.err(31592): at android.content.ContextWrapper.openFileInput(ContextWrapper.java:175)
01-21 19:19:29.530: W/System.err(31592): at com.activity.cardsnuovo.MainActivity.onCreate(MainActivity.java:91)
01-21 19:19:29.530: W/System.err(31592): at android.app.Activity.performCreate(Activity.java:5933)
01-21 19:19:29.530: W/System.err(31592): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
01-21 19:19:29.530: W/System.err(31592): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
01-21 19:19:29.530: W/System.err(31592): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
01-21 19:19:29.530: W/System.err(31592): at android.app.ActivityThread.access$800(ActivityThread.java:144)
01-21 19:19:29.530: W/System.err(31592): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
01-21 19:19:29.530: W/System.err(31592): at android.os.Handler.dispatchMessage(Handler.java:102)
01-21 19:19:29.530: W/System.err(31592): at android.os.Looper.loop(Looper.java:135)
01-21 19:19:29.530: W/System.err(31592): at android.app.ActivityThread.main(ActivityThread.java:5221)
01-21 19:19:29.530: W/System.err(31592): at java.lang.reflect.Method.invoke(Native Method)
01-21 19:19:29.530: W/System.err(31592): at java.lang.reflect.Method.invoke(Method.java:372)
01-21 19:19:29.530: W/System.err(31592): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:89)
01-21 19:19:29.530: W/System.err(31592): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-21 19:19:29.530: W/System.err(31592): Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
01-21 19:19:29.530: W/System.err(31592): at libcore.io.Posix.open(Native Method)
01-21 19:19:29.530: W/System.err(31592): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
01-21 19:19:29.530: W/System.err(31592): at libcore.io.IoBridge.open(IoBridge.java:442)
01-21 19:19:29.530: W/System.err(31592): ... 17 more
01-21 19:19:29.541: D/OpenGLRenderer(31592): Render dirty regions requested: true
01-21 19:19:29.547: D/Atlas(31592): Validating map...
01-21 19:19:29.572: I/Adreno-EGL(31592): <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/24/14, 167c270, I68fa98814b
01-21 19:19:29.572: I/OpenGLRenderer(31592): Initialized EGL, version 1.4
01-21 19:19:29.585: D/OpenGLRenderer(31592): Enabling debug mode 0
01-21 19:19:29.662: V/RenderScript(31592): Application requested CPU execution
01-21 19:19:29.666: V/RenderScript(31592): 0xa240c400 Launching thread(s), CPUs 4
答案 0 :(得分:1)
我认为文件名不同。你拼错了。这就是原因。
out = new OutputStreamWriter(openFileOutput("PosizioneTessereUtente.txt", MODE_APPEND));
这里的文件名是 PosizioneTessereUtente.txt
但是这里
InputStream inputPosizione = openFileInput("PosizioniTessereUtente.txt");
PosizioniTessereUtente.txt