gpt4 book ai didi

java - 如果我这样做的话,android 中创建的文件会在哪里

转载 作者:行者123 更新时间:2023-12-02 07:43:49 25 4
gpt4 key购买 nike

此函数创建一个文件,但我无法弄清楚该文件在哪里创建,如果有人有解决方案从外部存储在特定目录中创建文件,我们非常欢迎:)非常感谢

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();
}
}
}
}

最佳答案

查询

  • 文件将在哪里创建

它将按照函数名称所述在内部存储中创建,就像/data/data/yourApp_package_as_in_manifest/(可以在DDMS中看到)

查询

  • 如果有人有在特定目录中创建文件的解决方案非常欢迎来自外部存储的

根据链接Write a file in external storage in 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);
}

并向 list 添加 WRITE_EXTERNAL_STORAGE 权限

关于java - 如果我这样做的话,android 中创建的文件会在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11209429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com