0

次の行で、「Environment.getExternalStorageDirectory()」にファイルを作成しました

File dir = Environment.getExternalStorageDirectory();
File file = new File(dir,"/DCIM/"+fileTitle);

私の質問は、この作成されたファイルにテキストデータを書き込む方法ですか?

4

4 に答える 4

0

このチュートリアルから:

//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();}
于 2012-09-13T18:05:37.040 に答える
0

データをファイルに書き込む簡単なスニペットを次に示します。メイン UI スレッドでファイル IO を実行しないように、別のスレッドで実行するように設計されていることに注意してください。

new Thread(new Runnable() {
  public void run() {
    String FILENAME = "hello_file";
    String string = "hello world!";

    FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
    fos.write(string.getBytes());
    fos.close();
  }
}).start();
于 2012-09-13T18:25:58.023 に答える
0

FileWriterを使用してファイルに書き込むことができます

于 2012-09-13T18:01:30.567 に答える
0

テキストをファイルに出力するには、データを書き込むためのさまざまな便利なメソッドを持つPrintWriterクラスを使用できます。また、ストリームの概要を見ることができます

于 2012-09-13T18:03:08.090 に答える