+ Reply to Thread
Results 1 to 2 of 2

Thread: Read and write a text from file

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    28

    Read and write a text from file

    I`d like to know how can i read text from file and also how to write text to file and also with appending mode.

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    45
    Here is how you can read text and store it inside a scanner string :
    Code:
    File filesDir = getFilesDir();
    Scanner input = new Scanner(new File(filesDir, filename));
    Here is how you can write to text file :
    Code:
    String FILENAME = "file.txt";
    String string = "My text!";
    
    FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
    fos.write(string.getBytes());
    fos.close();
    Appending :
    Code:
    // true mean append
    BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
    buf.append(text);
    buf.newLine();
    buf.close();

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts