在Android中创建文件的问题
问题描述:
我想做一个简单的操作。在android中创建一个文件,但我不知道为什么。 android没有创建这个文件。 Android在调试2和调试3之间停止。文件夹未创建。我不明白为什么。在Android中创建文件的问题
try {
File inventoryDir = new File(Environment.getExternalStorageDirectory()+"/ludovic");
String userfile = "/session.txt";
if (!inventoryDir.exists()){
inventoryDir.mkdirs();
Log.i("debug","debug 1");
}
File userFile = new File(inventoryDir, userfile);
Log.i("debug","file path "+userFile.getAbsolutePath());
Log.i("debug","debug 2");
FileWriter fw = new FileWriter(userFile);
Log.i("debug","debug 3");
BufferedWriter bw = new BufferedWriter(fw);
Log.i("debug","debug 4");
bw.write("teste d'ecriture");
Log.i("debug","debug 5");
bw.close();
Log.i("debug","debug 6");
Log.i("debug","enregistre");
} catch (IOException e) {
Log.i("debug","non enregistre");
e.printStackTrace();
}
我有测试此代码
try { // catches IOException below
final String TESTSTRING = new String("Hello Android");
// ##### Write a file to the disk #####
/* We have to use the openFileOutput()-method
* the ActivityContext provides, to
* protect your file from others and
* This is done for security-reasons.
* We chose MODE_WORLD_READABLE, because
* we have nothing to hide in our file */
FileOutputStream fOut = openFileOutput("samplefile.txt",
MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
// Write the string to the file
osw.write(TESTSTRING);
/* ensure that everything is
* really written out and close */
osw.flush();
osw.close();
蒙山这段代码,我没有问题的创建文件,但如果我用Windows资源管理器搜索我没有找到这个文件。在清单文件
EDITED
File inventoryDir = new File(Environment.getExternalStorageDirectory()+"/ludovic");
if (!(inventoryDir.exists())){
inventoryDir.mkdir();
}
也WRITE_EXTERNAL_STORAGE权限:
答
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="byd.eagle"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".EagleBackup"
android:label="@string/app_name">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
看看这段代码示例。使用权限声明的位置是密钥。只要尝试进出活动!
+0
坦克的。我从来没有认为这是为了这个许可。 – Agriesean
答
更换
inventoryDir.mkdirs();
到
inventoryDir.mkdir();
最新
File inventoryDir = new File(Environment.getExternalStorageDirectory()+"/ludovic/");
String userfile = "session.txt";
if (!(inventoryDir.exists())){
inventoryDir.mkdir();
Log.i("debug","debug 1");
}
File userFile = new File(inventoryDir, userfile);
try {
PrintWriter out = new PrintWriter(mFile);
} catch (IOException e){
e.printStackTrace();
}
out.print("some text");
out.close();
}
我使用该权限 \t \t \t \t –
Agriesean
您可以发布Logcat内容,我们可以确切知道引发了什么异常。 – Aloong
这是一个警告:java.io.FileNotFoundException:/mnt/sdcard/ludovic/session.txt(权限被拒绝)。 – Agriesean