0

这就是我正在做的事情:

    static void writeToFile(String text) {
    try {
        synchronized (fileLock) {
            File external = Environment.getExternalStorageDirectory();
            String sdcardPath = external.getPath();
            FileWriter filewriter = new FileWriter(sdcardPath
                    + "/Documents/myfile.txt", true);
            BufferedWriter out = new BufferedWriter(filewriter);

            out.write(text);

            out.close();
            filewriter.close();
        }
    } catch (Exception e) {
        android.util.Log.d("failed to save file", e.toString());
    }
}

在打开的文件中,我得到以下异常:

java.io.FileNotFoundException: /mnt/sdcard/Documents/myfile.txt: 
    open failed: ENOENT (No such file or directory)
  1. 该目录/mnt/sdcard/Documents/根据Root Browses存在
  2. 应用程序清单确实包含<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  3. myfile.txt不存在,我希望创建或附加它

如何在 android 上创建或附加到文件?

更新 这是我的 AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mytest"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
4

4 回答 4

3

首先你必须创建文件然后附加到它这是创建文件的代码

    sFileName = "Android.txt";
    File root=Environment.getExternalStorageDirectory();

    File textfile = new File(root, sFileName);
    textfile.createNewFile();

允许:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2013-08-07T10:14:12.863 回答
1

像这样使用:

   File root = android.os.Environment.getExternalStorageDirectory(); 


   // See http://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();
   }   

或者:

     File direct = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/");

     File file = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/myFile.txt");        
     if(!direct.exists())
     {
         direct.mkdir();
     }        

     if (!file.exists()) {
             try {
                 file.createNewFile();
                 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 (IOException e) {
                 e.printStackTrace();
             }}

希望这会帮助你。

于 2013-08-07T10:44:02.510 回答
1

你可以尝试这样的事情:

static void writeToFile(String text) {
    try {
        synchronized (fileLock) {
            File external = Environment.getExternalStorageDirectory();
            String sdcardPath = external.getPath();
            File file = new File(sdcardPath + "/Documents/myfile.txt");
            file.createNewFile();
            FileWriter filewriter = new FileWriter(file);
            BufferedWriter out = new BufferedWriter(filewriter);

            out.write(text);

            out.close();
            filewriter.close();
        }
    } catch (Exception e) {
        android.util.Log.d("failed to save file", e.toString());
    }
}
于 2013-08-07T10:09:07.387 回答
1

尝试这个:

        File storagePath = new File(getExternalCacheDir() + "/dirname/");
        storagePath.mkdirs();
        File myFile = new File(storagePath, fileName);
        OutputStream out = new BufferedOutputStream(new FileOutputStream(
                myFile));
        try {

            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }

        } finally {
            try {
                out.close();

            } catch (IOException e) {

            }
            try {
                inputStream.close();
            } catch (IOException e) {

            }
        }
于 2013-08-07T10:39:02.820 回答