【问题标题】:i'm tring to create a file text我正在尝试创建文件文本
【发布时间】:2015-01-22 20:18:28
【问题描述】:

大家好,我是 java 和 android 世界的大佬。 这是我第一次在 stackoverflow.com 上寻求帮助

我正在尝试在我的第一个应用程序 android 中创建文件文本。

这是一个示例代码

String string = nome;
try {
    File file = new File("prova.txt");
    FileOutputStream fos =null;
    if (file.exists() ) {
       fos = new FileOutputStream("prova.txt", true);
        PrintWriter print = new PrintWriter(fos);
        print.append(nome);
        print.close();


    } else if (file.createNewFile()) {

        PrintWriter print = new PrintWriter(file);
        print.println(nome);
        print.close();
    }
}
catch (Exception e)
{
    e.printStackTrace();
}

应用程序不会创建文件。 你能帮帮我吗? 你能解释一下为什么吗?

非常感谢

【问题讨论】:

  • 看看这个答案它应该可以工作并且评论很好:stackoverflow.com/a/7718374/1173391
  • 还要确保添加允许您访问外部存储的权限:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

标签: android file


【解决方案1】:

尝试使用以下代码:http://www.anddev.org/working_with_files-t115.html

您可以随时进行更改以获得您的结果

//Writing a file...  



try { 
       // catches IOException below
       final String TESTSTRING = new String("Hello Android");

       /* 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();

//Reading the file back...

       /* We have to use the openFileInput()-method
        * the ActivityContext provides.
        * Again for security reasons with
        * openFileInput(...) */

        FileInputStream fIn = openFileInput("samplefile.txt");
        InputStreamReader isr = new InputStreamReader(fIn);

        /* Prepare a char-Array that will
         * hold the chars we read back in. */
        char[] inputBuffer = new char[TESTSTRING.length()];

        // Fill the Buffer with data from the file
        isr.read(inputBuffer);

        // Transform the chars to a String
        String readString = new String(inputBuffer);

        // Check if we read back the same chars that we had written out
        boolean isTheSame = TESTSTRING.equals(readString);

        Log.i("File Reading stuff", "success = " + isTheSame);

    } catch (IOException ioe) 
      {ioe.printStackTrace();}

【讨论】:

    【解决方案2】:

    欢迎来到Stackoverflow@Vincenzo Carriero :)

    我认为你错过了权限,否则你的代码看起来还不错。

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

    帮助链接:


    编辑:您是否调试过您的应用程序?如果遇到"java.io.IOException: open failed: EROFS (Read-only file system)"这样的异常,则说明你的文件路径有问题.您需要编写文件路径,如:

    String filePath =this.getFilesDir().getPath().toString()+"/fileName.txt";
    File f = new File(filePath);
    

    注意:我使用了**this**.getFilesDir(),因为我在Activity 中进行编码。 如果您使用上述 filePath 存储文件,您的文件将存储在:“/data/data/[package name]/[file name].txt”。

    请查看以下帮助链接:

    【讨论】:

    • @Vincenzo Carriero 您有实际设备还是正在使用模拟器?
    • 我正在使用模拟器。可能是问题吗?
    • @Vincenzo Carriero 你调试了吗,有什么异常吗?
    猜你喜欢
    • 2021-06-05
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 2021-06-21
    相关资源
    最近更新 更多