Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 文件未在SDCARD android上创建_Java_Android - Fatal编程技术网

Java 文件未在SDCARD android上创建

Java 文件未在SDCARD android上创建,java,android,Java,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);

嗨,我有下面的写文件的代码,它读取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();
    }
String filePath1=“/sdcard/Dictionarys/a.txt”;
字符串filePath2=“/sdcard/Dictionarys/b.txt”;
字符串filePath3=“/sdcard/Dictionarys/ab.xml”;
试一试{
File File=新文件(filePath3);
createNewFile();
BufferedReader br1=新的BufferedReader(新的InputStreamReader(
新文件输入流(文件路径1),UTF8),缓冲区大小;
BufferedReader br2=新的BufferedReader(新的InputStreamReader(
新文件输入流(文件路径2),UTF8),缓冲区大小;
FileOutputStream file3=新的FileOutputStream(filePath3);
OutputStreamWriter out3=新的OutputStreamWriter(文件3,UTF8);
BufferedWriter br3=新的BufferedWriter(out3,缓冲区大小);
字符串sCurrentLine1、sCurrentLine2;
而((sCurrentLine1=br1.readLine())!=null&((sCurrentLine2=br2.readLine())!=null)){
字符串s3=sCurrentLine2.substring(sCurrentLine1.length());
br3.写入(“\n”);
br3.flush();
i++;
}
br3.写入(“\n”);
br3.flush();
br3.close();
out3.flush();
out3.close();
file3.close();
}捕获(例外e){
e、 printStackTrace();
}

但xml文件不是在SD卡路径上创建的。

请尝试以下方式获取路径:

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

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

尝试通过以下方式获取路径:

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

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

首先,您不应该硬编码SD卡的路径,因为SD卡可以在不同的手机之间切换。 使用类似于
Context.getExternalFilesDir()
getExternalStoragePublicDirectory(String)
的方法


其次,一些文件系统具有缓冲。您应该使用
fsync
方法进行实际写入。有关更多信息,请参阅。

首先,您不应该硬编码SD卡的路径,因为SD卡可以在不同的手机之间切换。 使用类似于
Context.getExternalFilesDir()
getExternalStoragePublicDirectory(String)
的方法


其次,一些文件系统具有缓冲。您应该使用
fsync
方法进行实际写入。有关更多信息,请参阅。

您是否已授予写入外部存储器的权限?有时很容易忽略,或者尝试遵循此操作。请尽量不要使用硬编码SD卡路径。因为在某些设备中SD卡路径可以是/mnt/sdcard0/。您是否已授予写入外部存储器的权限?有时很容易忽略,或者尝试遵循此操作。请尽量不要使用硬编码SD卡路径。因为在某些设备中,SD卡路径可以是/mnt/sdcard0/。请注意,的文档指示您不应直接使用此方法,而应使用
Context.getExternalFilesDir()
getExternalStoragePublicDirectory(String)
代替。请注意,的文档指示您不应直接使用此方法,但是使用
Context.getExternalFilesDir()
getExternalStoragePublicDirectory(String)
代替标题,我对你的问题投了赞成票,因为我觉得它不值得投反对票,但要求这样做并不是一个好的做法。。。如果Usama Sarwar的回答对你有帮助的话,你也应该接受。我对你的问题投了更高的票,因为我觉得这不值得投反对票,但要求这样做并不是一个好的做法。。。如果Usama Sarwar的回答对你有帮助,你也应该接受