我想做一个简单的操作。在android中创建一个文件,但我不知道为什么。 android没有创建这个文件。 Android在调试2和调试3之间停止。该文件夹未创建。我不明白为什么。
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资源管理器搜索,我找不到此文件。
答案 0 :(得分:0)
替换
inventoryDir.mkdirs();
到
inventoryDir.mkdir();
最新:
File inventoryDir = new File(Environment.getExternalStorageDirectory()+"/ludovic");
if (!(inventoryDir.exists())){
inventoryDir.mkdir();
}
以及清单文件中的WRITE_EXTERNAL_STORAGE权限
编辑:
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();
}
答案 1 :(得分:0)
你可以试试这个:
File inventoryDir = new File(Environment.getExternalStorageDirectory()+"/ludovic/");
String userfile = "session.txt";
答案 2 :(得分:0)
<?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" />
看看这段代码示例。声明的uses-permission的位置是密钥。只需尝试进出活动!