我正在尝试创建文件文本

时间:2015-01-22 20:18:29

标签: android file

嗨,大家好,我是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();
}

该应用无法创建该文件。 你能帮我个忙吗? 你能解释一下为什么吗?

非常感谢

2 个答案:

答案 0 :(得分:0)

尝试使用以下代码: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();}

答案 1 :(得分:0)

欢迎来到Stackoverflow @Vincenzo Carriero:)

我认为您错过了权限,否则您的代码似乎没问题。

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

帮助链接:


修改:您是否调试了应用程序?如果您遇到异常,例如 “java.io.IOException:open failed:EROFS(只读文件系统)” ,则表示文件路径存在问题。您需要编写文件路径,如:

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

注意:我使用了**this**.getFilesDir()因为我在 Activity 中编码。 如果使用上述 filePath 存储文件,则您的文件将存储在:“ / data / data / [包名称] / [文件名] .txt ”。< / p>

请查看以下帮助链接: