我面临将外部数据存储到外部文本文件中的问题。我的代码如下,但它总是给我 IOException 案例。我没有看到在目录中创建任何文件。应用程序只是自行关闭,是否可以保持打开状态?为什么?
private void RecordData() {
// TODO Auto-generated method stub
myFile = new File("sdcard/suntrackingdata.txt");
DataSeq = countDis[9] + "," + yawDis[9] + "," + yawdiffDis[9] + "," + AziIniDis[9] + "," + AziFinDis[9] + "," + AxDis[9] + "," + AyDis[9] + "," + ThetaDis[9] + "," + ThetaDiffDis[9] + "," + yawONEdecDis[9] + "," + yawNONdecDis[9] ;
//if(myFile.exists() == false)
//{
// myFile.mkdirs();
//}
try{
myFile.createNewFile();
FileOutputStream fos = new FileOutputStream(myFile);
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.append(DataSeq);
osw.close();
Toast.makeText(getBaseContext(), "Recording... ",
Toast.LENGTH_SHORT).show();
} catch (IOException e)
{
Toast.makeText(getBaseContext(), "Failed to write file...", Toast.LENGTH_SHORT).show();
}
}
当我打开录音时,我在 logcat 中得到了这个。
07-03 22:32:33.068: W/System.err(4764): java.io.FileNotFoundException: /sdcard/suntrackingdata.txt: open failed: EISDIR (Is a directory)
07-03 22:32:33.068: W/System.err(4764): at libcore.io.IoBridge.open(IoBridge.java:409)
07-03 22:32:33.068: W/System.err(4764): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
07-03 22:32:33.068: W/System.err(4764): at com.example.suntracking.MainActivity.RecordData(MainActivity.java:536)
07-03 22:32:33.068: W/System.err(4764): at com.example.suntracking.MainActivity.access$4(MainActivity.java:526)
07-03 22:32:33.068: W/System.err(4764): at com.example.suntracking.MainActivity$1$1.run(MainActivity.java:109)
07-03 22:32:33.068: W/System.err(4764): at android.os.Handler.handleCallback(Handler.java:730)
07-03 22:32:33.068: W/System.err(4764): at android.os.Handler.dispatchMessage(Handler.java:92)
07-03 22:32:33.068: W/System.err(4764): at android.os.Looper.loop(Looper.java:137)
07-03 22:32:33.068: W/System.err(4764): at android.app.ActivityThread.main(ActivityThread.java:5414)
07-03 22:32:33.068: W/System.err(4764): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 22:32:33.068: W/System.err(4764): at java.lang.reflect.Method.invoke(Method.java:525)
07-03 22:32:33.068: W/System.err(4764): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
07-03 22:32:33.068: W/System.err(4764): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
07-03 22:32:33.068: W/System.err(4764): at dalvik.system.NativeStart.main(Native Method)
07-03 22:32:33.068: W/System.err(4764): Caused by: libcore.io.ErrnoException: open failed: EISDIR (Is a directory)
07-03 22:32:33.068: W/System.err(4764): at libcore.io.Posix.open(Native Method)
07-03 22:32:33.068: W/System.err(4764): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
07-03 22:32:33.068: W/System.err(4764): at libcore.io.IoBridge.open(IoBridge.java:393)
07-03 22:32:33.068: W/System.err(4764): ... 13 more
我的最新代码
private void RecordData() {
// TODO Auto-generated method stub
externalPath = Environment.getExternalStorageDirectory();
myFile = new File(externalPath,"suntrackingdata.txt");
DataSeq = countDis[9] + "," + yawDis[9] + "," + yawdiffDis[9] + "," + AziIniDis[9] + "," + AziFinDis[9] + "," + AxDis[9] + "," + AyDis[9] + "," + ThetaDis[9] + "," + ThetaDiffDis[9] + "," + yawONEdecDis[9] + "," + yawNONdecDis[9] ;
/*if(myFile.exists() == false)
{
myFile.mkdirs();
}*/
try{
myFile.createNewFile();
FileOutputStream fos = new FileOutputStream(myFile,true);
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(DataSeq);
bw.close();
fos.close();
Toast.makeText(getBaseContext(), "Recording... ",
Toast.LENGTH_SHORT).show();
}catch (FileNotFoundException e)
{
Toast.makeText(getBaseContext(), "File Not Found", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
Toast.makeText(getBaseContext(), "IOException", Toast.LENGTH_SHORT).show();
}
}