如何建立在Android设备的根目录/写文件?根目录、文件、设备、Android

2023-09-12 03:44:39 作者:③分愛人七分愛己

我发现,您可以使用像这样创建一个文件:

I found out that you can use something like this to create a file:

FileOutputStream  fs = openFileOutput("/test.in", MODE_WORLD_WRITEABLE);
String s = "[Head]\r\n";
s += "Type=2";
byte[] buffer = s.getBytes();
fs.write(buffer);
fs.close();

在运行上面的code,我得到一个IllegalArgumentException说明:

When running the above code I get an IllegalArgumentException stating:

java.lang.IllegalArgumentException异常:   文件/test.in包含一个路径   分离

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

和即时猜测的/是不是AP preciated。我想要的/,因为我需要写文件到设备的根目录下,如在API中规定在努力遵循:

and Im guessing the "/" is not appreciated. I wanted the "/" since I need to write the file to the root directory of the device, as stated in the API in trying to follow:

一个请求是一个文本文件(UNI code)与   文件扩展名。在。该   应用程序读取并分析。在   当它放置在根文件   在移动设备上的目录。

A request is a textfile (UNICODE) with the file extension ".in". The application reads and parses the .in file when it's placed in root directory on the mobile device.

问题是:我如何在根目录中的文件?我一直在四处寻找一个答案,但还没有找到过。

Question is: how do I place a file in the root-directory? I have been looking around for an answer, but havent found one yet.

关于

推荐答案

Context.openFileOutput是指用于创建文件专用于您的应用程序。他们走在你的应用程序的私有数据目录。你提供一个的名称的,而不是一个路径:命名文件的打开的名称;不能包含路径分隔符

Context.openFileOutput is meant to be used for creating files private to your application. they go in your app's private data directory. you supply a name, not a path: "name The name of the file to open; can not contain path separators".

http://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String, INT)

对于你的问题,你不能写入/除非你是根:

as for your question, you can't write to / unless you're root:

我的Linux的盒子$亚行外壳的ls -l -d / drwxr-XR-X根根2010-01-16 07:42 $

my-linux-box$ adb shell ls -l -d / drwxr-xr-x root root 2010-01-16 07:42 $

我不知道你的API是什么期待你写的根目录下,但我猜这不是一个Android API和你读错的文件; - )

i don't know what your API is that expects you to write to the root directory, but i'm guessing it's not an Android API and you're reading the wrong documentation ;-)