写入到SD卡
我有一个非常小的问题。我写一个文件到SD卡。 I'l显示两个code,一是code的作品和其他没有。它看起来像这样:
第一个,
i have a very small problem. I am writing a file to sdcard. I'l show you two code, one code works and other doesn't. It looks like this: First one,
new FileOutputStream("/sdcard/HelloWorld.txt")
这工作正常,并在SD卡创建HelloWorld.txt文件。
This works fine and creates a HelloWorld.txt file in sdcard.
现在第二个,
new FileOutputStream(android.os.Environment.getExternalStorageDirectory()+java.io.File.separator + "filetest" + java.io.File.separator + "HelloWorld.txt")
这会引发错误 /mnt/sdcard/filetest/HelloWorld.txt(没有这样的文件或目录)
。
我想知道为什么,因为我在我的设备上有MNT / SD卡路径,难道是它找不到filetest文件夹,如果是的话那么是不是应该创建filetest文件夹,如果它不是之前创建的。
This throws error "/mnt/sdcard/filetest/HelloWorld.txt (No such file or directory)
".
I want to know why because i have mnt/sdcard path on my device, is it that it cannot find filetest folder if yes then isn't it supposed to create filetest folder if its not created before.
感谢。
先 filetest
如果某个目录及其不可用,
First Make a directory of filetest
if its not available,
File file = new File(android.os.Environment.getExternalStorageDirectory()+java.io.File.separator + "filetest");
file.mkdir();
然后执行您的code ...
Then execute your code...
或
File f = new File(android.os.Environment.getExternalStorageDirectory()+java.io.File.separator + "filetest" + java.io.File.separator + "HelloWorld.txt");
if (!f.getParentFile().exists());
{
f.getParentFile().mkdir();
}