如何在android工作室中创建文件到新文件夹



我仍然没有看到创建任何文件夹或文件。哪里出了问题。

try {
String rootPath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/MyFolder/";
File root = new File(rootPath);
if (!root.exists()) {
root.mkdirs();
}
File f = new File(rootPath + "mttext.txt");
if (f.exists()) {
f.delete();
}
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}

求你了,出什么问题了。

试试这个并检查!

try {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/newfoldername/";    // it will return root directory of internal storage
File root = new File(path);
if (!root.exists()) {
root.mkdirs();       // create folder if not exist 
}
File file = new File(rootPath + "log.txt");
if (!file.exists()) {
file.createNewFile();   // create file if not exist 
}
BufferedWriter buf = new BufferedWriter(new FileWriter(file, true));
buf.append("hi this will write in to file");
buf.newLine();  // pointer will be nextline
buf.close();
} 
catch (Exception e) {
e.printStackTrace();
}

添加你的AndroidManifest.xml

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

最新更新