如何在 Android 设备的根目录中创建/写入文件? [英] How to create/write file in the root of the Android device?

查看:29
本文介绍了如何在 Android 设备的根目录中创建/写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现你可以使用这样的东西来创建一个文件:

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]
";
s += "Type=2";
byte[] buffer = s.getBytes();
fs.write(buffer);
fs.close();

当运行上面的代码时,我得到一个 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

我猜/"不受欢迎.我想要/",因为我需要将文件写入设备的根目录,如 API 中所述:

and I'm 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:

请求是一个文本文件(UNICODE),其中包含文件扩展名.in".这应用程序读取并解析 .in文件放在根目录中时移动设备上的目录.

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 haven't 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)

至于你的问题,除非你是 root,否则你不能写信给/:

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

my-linux-box$ adb shell 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 ;-)

这篇关于如何在 Android 设备的根目录中创建/写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆