[英]Where will be a file created in android if I do it like this
This function creates a file but I can't figure out where is the file created and if someone has a solution to create a file in a particular directory from the external storage is very welcomed :) thanks a lot 这个函数创建一个文件,但我无法弄清楚文件在哪里创建,如果有人有解决方案从外部存储创建特定目录中的文件非常欢迎:)非常感谢
private void writeFileToInternalStorage() {
String eol = System.getProperty("line.separator");
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(openFileOutput("myfile", MODE_WORLD_WRITEABLE)));
writer.write("This is a test1." + eol);
writer.write("This is a test2." + eol);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
openFileOutput
always create files in your local project directory ie same as what is returned by getFilesDir()
openFileOutput
总是在本地项目目录中创建文件,即与getFilesDir()
返回的文件相同
Your file will be created at /data/data/your.package.name/
您的文件将在
/data/data/your.package.name/
创建
To write to extrnal storage you will need to do something like this. 要写入extrnal存储,您需要执行类似的操作。
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdcard.getAbsolutePath() + "/MyDir");
dir.mkdirs();
File file = new File(dir, "filename");
FileOutputStream f = new FileOutputStream(file);
...
Finally don't forget to include 最后别忘了包括
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
in manifest. 在清单中。
It will be created on internal folder: /data/data/com.package.name/
You cannot access that folder using file browser. 它将在内部文件夹中创建:
/data/data/com.package.name/
您无法使用文件浏览器访问该文件夹。
If you want to easily access the file you can try to create it on SD card: 如果您想轻松访问该文件,可以尝试在SD卡上创建它:
/*...*/
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = baseDir + "/"+ "myFile.txt";
FileOutputStream writer = null;
try {
writer = new FileOutputStream(fileName);
writer.write("This is a test1." + eol);
/*...*/
for query 用于查询
it will create in Internal Storage as function name said and that will be like /data/data/yourApp_package_as_in_manifest/ (can see in DDMS) 它将在内部存储中创建,如函数名称所示,它将类似于/ data / data / yourApp_package_as_in_manifest /(可以在DDMS中看到)
for query 用于查询
as per link Write a file in external storage in Android ......... 根据链接在Android中的外部存储中写入文件 .........
** Method to check whether external media available and writable. This is adapted from
http://developer.android.com/guide/topics/data/data-storage.html#filesExternal */
private void checkExternalMedia(){
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// Can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// Can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Can't read or write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
tv.append("\n\nExternal Media: readable="
+mExternalStorageAvailable+" writable="+mExternalStorageWriteable);
}
/** Method to write ascii text characters to file on SD card. Note that you must add a
WRITE_EXTERNAL_STORAGE permission to the manifest file or this method will throw
a FileNotFound Exception because you won't have write permission. */
private void writeToSDFile(){
// Find the root of the external storage.
// See http://developer.android.com/guide/topics/data/data- storage.html#filesExternal
File root = android.os.Environment.getExternalStorageDirectory();
tv.append("\nExternal file system root: "+root);
// See https://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder
File dir = new File (root.getAbsolutePath() + "/download");
dir.mkdirs();
File file = new File(dir, "myData.txt");
try {
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
pw.println("Hi , How are you");
pw.println("Hello");
pw.flush();
pw.close();
f.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.i(TAG, "******* File not found. Did you" +
" add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
} catch (IOException e) {
e.printStackTrace();
}
tv.append("\n\nFile written to "+file);
}
and also add a WRITE_EXTERNAL_STORAGE permission to the manifest 并且还向清单添加WRITE_EXTERNAL_STORAGE权限
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.