Read Write File Manager : File « File « Android






Read Write File Manager

    

//package com.application.esmaker.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class ReadWriteFileManager {

  public void start(Context cx) {
    final String TEMP_FILE_NAME = "temp.txt";
    String contentToWrite = "THIS IS THE CONTENT TO WRITE!";

    Log.d("DEBUG", "Instantiating ReadWriteManager..");

    ReadWriteFileManager readWriteFileManager = new ReadWriteFileManager();

    Log.d("DEBUG", "Writing to file...");

    readWriteFileManager.writeToFile(cx, contentToWrite.getBytes(),
        TEMP_FILE_NAME);

    Log.d("DEBUG", "Reading from file..");

    String contentRead = readWriteFileManager.readFromFile(cx,
        TEMP_FILE_NAME);

    Log.d("DEBUG", "Content read: " + contentRead);

    Toast.makeText(cx, contentRead, Toast.LENGTH_LONG).show();
  }

  public String readFromFile(Context context, String sourceFileName) {
    /*
     * Reading will be file type specific.. Here I'm considering text file
     * alone..
     */
    FileInputStream fis = null;
    InputStreamReader isr = null;

    char[] inputBuffer = null;
    String file_content = null;

    try {
      // As this requires full path of the file...
      // i.e data/data/package_name/files/your_file_name
      File sourceFile = new File(context.getFilesDir().getPath()
          + File.separator + sourceFileName);

      if (sourceFile.exists()) {
        // Probably you will get an exception if its
        // a huge content file..
        // I suggest you to handle content here
        // itself, instead of
        // returning it as return value..
        inputBuffer = new char[(int) sourceFile.length()];

        fis = context.openFileInput(sourceFileName);

        isr = new InputStreamReader(fis);
        isr.read(inputBuffer);

        file_content = new String(inputBuffer);

        try {
          isr.close();
          fis.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
      file_content = null;
    }

    return file_content;
  }

  public boolean writeToFile(Context context, byte[] contentToWrite,
      String destinationFileName) {
    FileOutputStream fos = null;
    boolean retValue = false;
    try {
      // Note that your file will be created and stored
      // under the path: data/data/your_app_package_name/files/
      fos = context.openFileOutput(destinationFileName,
          Context.MODE_PRIVATE);

      // Note that we are not buffering of contents to write..
      // So, this will work best for files with less content....
      fos.write(contentToWrite);
      // Successfully completed writing..
      retValue = true;
      try {
        fos.flush();
        fos.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    } catch (Exception e) {
      // You can catch exceptions specifically for low mem,
      // file cant be created, and so on..
      e.printStackTrace();
      // Failed to write..
      retValue = false;
    }

    return retValue;
  }

}

   
    
    
    
  








Related examples in the same category

1.Read the created file and display to the screen
2.Create a Uri for files on external storage
3.Create a Uri for files on internal storage
4.File read and write
5.View/Listen to media file
6.File Copy Util
7.Copy File Thread
8.Adaptation of the class File to add some usefull functions
9.Save to Android local file system
10.Using Ini file to manage Preferences
11.File Cache
12.Format File Size
13.File Upload
14.File Name Extension Utils
15.Write and append string to file
16.Transfers required files to the the private file system part in order to be access them from C Code.
17.returns a canonical line of a obj or mtl file.
18.Trims down obj files, so that they may be parsed faster later on.
19.Format File Size:KB, MB, GB
20.Get File Contents as String
21.Read File and return CharSequence
22.Copy File
23.Read/write From FileSystem, browse Account
24.Create Path and file under External Storage
25.Get File Extension Name
26.Extract File Name From String
27.Write String to file
28.Create an output file from raw resources.
29.Copies a directory from a jar file to an external directory.
30.Reads a file from /raw/res/ and returns it as a String
31.Read Config ini file
32.Copy two files
33.Get the filename of the media file and use that as the default title
34.Copy File and Directory
35.Determines the MIME type for a given filename.
36.Move a file stored in the cache to the internal storage of the specified context
37.CharSequence from File
38.File Manager
39.Save Exception to a file
40.Delete a file and create directory
41.Get a list of filenames in this folder.
42.Delete Directory
43.Delete Directory 2
44.Delete Directory 3
45.Get readable folder size
46.Copy chars from a large (over 2GB) Reader to a Writer.