0

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

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

3 回答 3

2

openFileOutput始终在本地项目目录中创建文件,即与返回的文件相同 getFilesDir()

您的文件将创建于 /data/data/your.package.name/

要写入外部存储,您需要执行类似的操作。

File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdcard.getAbsolutePath() + "/MyDir");
dir.mkdirs();
File file = new File(dir, "filename");

FileOutputStream f = new FileOutputStream(file);
...

最后别忘了包括

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

在清单中。

于 2012-06-26T14:22:12.083 回答
0

它将在内部文件夹上创建:/data/data/com.package.name/您无法使用文件浏览器访问该文件夹。

如果您想轻松访问该文件,您可以尝试在 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);
    /*...*/
于 2012-06-26T14:25:06.123 回答
0

查询

  • 将在哪里创建文件

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

查询

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

根据链接在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);
    }

并向清单添加 WRITE_EXTERNAL_STORAGE 权限

于 2012-06-26T14:25:13.987 回答