将文件写入android中的sdcard

时间:2013-12-04 07:44:56

标签: android file android-sdcard

我想在SD卡上创建一个文件。在这里我可以创建文件并将其读/写到应用程序,但我想要的是,文件应该保存在SD卡的特定文件夹中。如何使用FileOutputStream

执行此操作
// create file
    public void createfile(String name) 
    {
        try
        {
            new FileOutputStream(filename, true).close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // write to file

    public void appendToFile(String dataAppend, String nameOfFile) throws IOException 
    {
        fosAppend = openFileOutput(nameOfFile, Context.MODE_APPEND);
        fosAppend.write(dataAppend.getBytes());
        fosAppend.write(System.getProperty("line.separator").getBytes());
        fosAppend.flush();
        fosAppend.close();
    }

4 个答案:

答案 0 :(得分:7)

以下是我的代码中的示例:

try {
    String filename = "abc.txt";
    File myFile = new File(Environment
            .getExternalStorageDirectory(), filename);
    if (!myFile.exists())
        myFile.createNewFile();
    FileOutputStream fos;
    byte[] data = string.getBytes();
    try {
        fos = new FileOutputStream(myFile);
        fos.write(data);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

不要忘记:

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

答案 1 :(得分:5)

试试这个,

try {
    File newFolder = new File(Environment.getExternalStorageDirectory(), "TestFolder");
    if (!newFolder.exists()) {
        newFolder.mkdir();
    }
    try {
        File file = new File(newFolder, "MyTest" + ".txt");
        file.createNewFile();
    } catch (Exception ex) {
        System.out.println("ex: " + ex);
    }
} catch (Exception e) {
    System.out.println("e: " + e);
}

答案 2 :(得分:1)

在android

中的sd卡上创建文件夹和写文件非常容易

代码段

    String ext_storage_state = Environment.getExternalStorageState();
            File mediaStorage = new File(Environment.getExternalStorageDirectory()
                    + "/Folder name");
            if (ext_storage_state.equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
                if (!mediaStorage.exists()) {
                    mediaStorage.mkdirs();
                } 
                //write file writing code..

try {

                FileOutputStream fos=new FileOutputStream(file name);
                try {
                    fos.write(filename.toByteArray());
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }           
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
            else
            {
                //Toast message sd card not found..
            }

答案 3 :(得分:0)

注意:&#39; getExternalStorageDirectory()&#39; 实际上为您提供了/ storage / emulated / 0的路径,而不是实际的SD卡

&#39; String path = Syst.envr(&#34; SECONDARY_STORAGE&#34;);&#39;为您提供SD卡的路径