繁体   English   中英

不是在SDCARD android上创建文件

[英]file is not creating on SDCARD android

嗨,我有以下代码用于写文件,该文件读取a和b文件并将数据放入ab.xml文件中

   String filePath1 = "/sdcard/Dictionarys/a.txt";
String filePath2 = "/sdcard/Dictionarys/b.txt";
String filePath3 = "/sdcard/Dictionarys/ab.xml";


        try {

        File file = new File(filePath3);
        file.createNewFile();

        BufferedReader br1 = new BufferedReader(new InputStreamReader(
                new FileInputStream(filePath1), UTF8), BUFFER_SIZE);

        BufferedReader br2 = new BufferedReader(new InputStreamReader(
                new FileInputStream(filePath2), UTF8), BUFFER_SIZE);
        FileOutputStream file3 = new FileOutputStream(filePath3);
        OutputStreamWriter out3 = new OutputStreamWriter(file3,UTF8);
        BufferedWriter br3 = new BufferedWriter(out3, BUFFER_SIZE);

        String sCurrentLine1, sCurrentLine2;

        while ((sCurrentLine1 = br1.readLine()) != null && ((sCurrentLine2 = br2.readLine())!=null)) {
            String s3 = sCurrentLine2.substring(sCurrentLine1.length());
            br3.write("<abcd abc=\""+sCurrentLine1+"\" def=\""+s3.trim()+"\"/> \n");
            br3.flush();
            i++;
        }
        br3.write("<data>\n");
        br3.flush();
        br3.close();
        out3.flush();
        out3.close();
        file3.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

但未在SDCARD路径上创建xml文件。

尝试通过以下方式获取Path:

 Environment.getExternalStorageDirectory()

有时,字符串路径不起作用,请确保您在清单中添加了以下权限。

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

首先, 您不应该对sdcard的路径进行硬编码 ,因为sdcard可能在手机之间变化。 使用类似Context.getExternalFilesDir()getExternalStoragePublicDirectory(String)

其次,某些文件系统具有buffering 您应该使用fsync方法进行实际写入。 有关更多信息,请参见安全保存数据

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM