1

可能重复:
Android 写入 sd 卡文件夹

使用此代码编写文件:

public void writeFile(String data){
try { // catches IOException below
final String TESTSTRING = new String(data+"-");


String path=Environment.getExternalStorageDirectory().getAbsolutePath()
    + "/test.txt";
FileOutputStream fOut = openFileOutput(path,
                                        MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut); 

// Write the string to the file
osw.write(TESTSTRING);
/* ensure that everything is
 * really written out and close */
osw.flush();
osw.close();

}catch (Exception e) {
    e.printStackTrace();
}
}

以上代码产生的错误:

java.lang.IllegalArgumentException: File /sdcard/test.txt contains a path separator  

我找不到这段代码有什么问题。

4

2 回答 2

1

openFileOutputis 用于为应用程序创建私有文件,这些文件将保存在应用程序的私有目录中。这意味着您需要将文件名发送到方法而不是整个路径。

确保您 在AndroidManifest.xml.

于 2011-07-15T05:11:54.850 回答
0

openFileOutput()不接受路径,只接受文件名。

于 2011-07-15T05:08:56.743 回答