Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.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
Java android:如何在android-2.3.7下创建文件?_Java_Android_File - Fatal编程技术网

Java android:如何在android-2.3.7下创建文件?

Java android:如何在android-2.3.7下创建文件?,java,android,file,Java,Android,File,我在任何地方都找不到如何在android 2.3.7(Cyanogenmod)中正确创建新文件 普通代码不起作用,抛出IOException或FileNotFoundException: String path = Environment.getExternalStorageDirectory().getPath() + "/gpx/"; //There is a gpx folder in my sdcard, the app can read those files but can't cr

我在任何地方都找不到如何在android 2.3.7(Cyanogenmod)中正确创建新文件

普通代码不起作用,抛出IOException或FileNotFoundException:

String path = Environment.getExternalStorageDirectory().getPath() + "/gpx/";
//There is a gpx folder in my sdcard, the app can read those files but can't create a new one.

File file = new File(path, filename);
//if i insert file.createNewFile(); the app throws "IOException: Invalid argument" but does not crash.
FileOutputStream fos = new FileOutputStream(file);
String data = "somedata";
fos.write(data.getBytes());
fos.close();
输出流是公共的。当服务停止时,最终数据由onDestroy()中的另一种方法写入。在fos.write(endtag.getbytes())行的该方法中导致应用程序崩溃;错误为NullPointerException

据我所知,前面的代码没有创建文件输出流,因为无法创建文件。如果有人知道,什么是“IOException:无效参数”。那无效的论点是什么?我想我不明白问题出在哪里,也许?在该版本的android中创建文件时是否需要一些额外的参数?在安卓4+上,它工作正常


谁能给出一些基本的例子,如何在旧版本的android中在sdcard上创建文件,或者解释IOException中的“无效参数”是什么?

最后我找到了问题所在。在这个问题中,我忘了提到,使用的文件名是一个日期,格式为
“yyyy-mm-dd HH:mm:ss”
,所以问题是“:”符号,我建议在android 2.3.7的文件名中不允许使用该符号

简单地将格式修改为“yyyy-mm-dd-HH-mm-ss”就解决了这样一个难题