在android中创建文件的问题

发布于 2024-12-06 15:41:19 字数 2247 浏览 6 评论 0原文

我想做一个简单的操作。在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゝ杯具 2024-12-13 15:41:19
<?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" />

看看这个代码示例。声明使用权限的位置是关键。只需在活动中和活动外都尝试一下即可!

<?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" />

Look at this code sample. The location where the uses-permission declared is the key. Just try both in and out the activity!

酷到爆炸 2024-12-13 15:41:19

你能试试这个吗:

File inventoryDir = new File(Environment.getExternalStorageDirectory()+"/ludovic/");
String userfile = "session.txt";

Can you try this:

File inventoryDir = new File(Environment.getExternalStorageDirectory()+"/ludovic/");
String userfile = "session.txt";
楠木可依 2024-12-13 15:41:19

替换

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();
}

replace

inventoryDir.mkdirs();

to

inventoryDir.mkdir();

Latest:

 File inventoryDir = new File(Environment.getExternalStorageDirectory()+"/ludovic");
      if (!(inventoryDir.exists())){
      inventoryDir.mkdir();
      }

and also WRITE_EXTERNAL_STORAGE permission in Manifest file

EDITED:

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文