如何在 Android 设备的根目录中创建/写入文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2079766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create/write file in the root of the Android device?
提问by Ted
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();
When running the above code I get an IllegalArgumentException stating:
运行上述代码时,我收到一个 IllegalArgumentException 说明:
java.lang.IllegalArgumentException: File /test.in contains a path separator
java.lang.IllegalArgumentException: 文件 /test.in 包含路径分隔符
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:
我猜“/”不受欢迎。我想要“/”,因为我需要将文件写入设备的根目录,如 API 中所述:
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.
请求是文件扩展名为“.in”的文本文件 (UNICODE)。当 .in 文件位于移动设备的根目录中时,应用程序会读取并解析该文件。
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.
问题是:如何在根目录中放置文件?我一直在寻找答案,但还没有找到。
回答by Elliott Hughes
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".
Context.openFileOutput 旨在用于创建应用程序私有的文件。它们位于您应用的私有数据目录中。您提供name,而不是路径:“name 要打开的文件的名称;不能包含路径分隔符”。
http://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String, int)
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:
至于你的问题,除非你是 root,否则你不能写信给 /:
my-linux-box$ adb shell ls -l -d / drwxr-xr-x root root 2010-01-16 07:42 $
my-linux-box$ adb shell ls -l -d / drwxr-xr-x root root 2010-01-16 07:42 $
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 ;-)
我不知道您希望您写入根目录的 API 是什么,但我猜它不是 Android API 并且您正在阅读错误的文档;-)
回答by user865197
You can add files with path in private directory like that
您可以像这样在私有目录中添加带有路径的文件
String path = this.getApplicationContext().getFilesDir() + "/testDir/";
File file = new File(path);
file.mkdirs();
path += "testlab.txt";
OutputStream myOutput;
try {
myOutput = new BufferedOutputStream(new FileOutputStream(path,true));
write(myOutput, new String("TEST").getBytes());
myOutput.flush();
myOutput.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
回答by m4n07
I faced the same problem today
我今天遇到了同样的问题
java.lang.IllegalArgumentException: File /test.txt contains a path separator
java.lang.IllegalArgumentException: 文件 /test.txt 包含路径分隔符
and when i tried the following it worked.
当我尝试以下操作时,它起作用了。
File inputFile = new File("/storage/new/test.txt");
FileInputStream isr = new FileInputStream(inputFile);
DataInputStream in = new DataInputStream(isr);
if(!inputFile.exists()){
Log.v("FILE","InputFile not available");
}
else
{
while((line = in.readLine()) != null)
{
........ //parse
}
}
(Btw, I was getting this problem outside /root dir and while searching i saw this post)
(顺便说一句,我在 /root 目录之外遇到了这个问题,在搜索时我看到了这篇文章)