[英]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.