在android中创建文件的问题
我想做一个简单的操作。在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 资源管理器搜索,我找不到这个文件。
i would like to do a simple operation. Create a file in android, but i don't know why. android didn't create this file. Android stop between debug 2 and debug 3.The folder is not created. I don't understand why.
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();
}
i have test this code
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();
Whith this code, i didn't have problem's to create file but i don't find this file if i search with windows explorer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看这个代码示例。声明使用权限的位置是关键。只需在活动中和活动外都尝试一下即可!
Look at this code sample. The location where the uses-permission declared is the key. Just try both in and out the activity!
你能试试这个吗:
Can you try this:
替换
为
最新:
以及清单文件中的 WRITE_EXTERNAL_STORAGE 权限
编辑:
replace
to
Latest:
and also WRITE_EXTERNAL_STORAGE permission in Manifest file
EDITED: