Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
FileOutputStream不做任何事情(Android Studio)_Android_File - Fatal编程技术网

FileOutputStream不做任何事情(Android Studio)

FileOutputStream不做任何事情(Android Studio),android,file,Android,File,我有这种方法将一些记录的数据写入sd卡。但是,它甚至不创建文件 public void writeToFile(LinkedList<double[]> data, String name) throws IOException { String dataS = ""; dataS += "x;y;z;size\n"; for (int i = 0; i < data.size() - 1; i++) { dataS += data.g

我有这种方法将一些记录的数据写入sd卡。但是,它甚至不创建文件

 public void writeToFile(LinkedList<double[]> data, String name) throws IOException {
    String dataS = "";
    dataS += "x;y;z;size\n";
    for (int i = 0; i < data.size() - 1; i++) {
        dataS += data.get(i)[0] + ";" + data.get(i)[1] + ";" + data.get(i)[2] + ";" + data.get(i)[3] + "\n";
    }

    try {
        String filename = name + ".csv";

        File myFile = new File(Environment
                .getExternalStorageDirectory(), filename);

        if (!myFile.exists()) {
            myFile.createNewFile();
        }

        FileOutputStream fos;

        System.out.println(myFile.getAbsolutePath());
        byte[] dataByte = dataS.getBytes();
        try {
            fos = new FileOutputStream(myFile);
            fos.write(dataByte);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}
public void writeToFile(LinkedList数据、字符串名称)引发IOException{
字符串数据=”;
数据+=“x;y;z;大小\n”;
对于(int i=0;i
我已在Manifest.xml中添加了正确的权限:

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

在根目录中创建如下文件

File myFile = new File(Environment.getExternalStorageDirectory()+"/"+filename);
删除此代码

if (!myFile.exists()) {
   myFile.createNewFile();
}

您正在捕获一个异常并执行
e.printStackTrace
-是否检查是否发生了这种情况?logcat中有任何内容吗?我有,没有打印任何异常。我的建议是:在代码中放置一个断点并逐步执行
System.out.println(myFile.getAbsolutePath())的输出是什么
?/storage/emulated/0/com.mbientlab.metawear.MetaWearBleService$DefaultMetaWearBoardAndroidImpl@3935adca.csvStill没有创建文件。我认为这不会有帮助,文件名
com.mbientlab.metawear.MetaWearBleServic中存在问题‌​e$b‌​rdAndroidImpl@3935ad‌​ca.csv
不合法,一旦初始化
FileOutputStream
,就会抛出
FileNotFoundException