FileManager.java :  » UnTagged » metzoid » com » metzoid » files » Android Open Source

Android Open Source » UnTagged » metzoid 
metzoid » com » metzoid » files » FileManager.java
/**
 * 
 */
package com.metzoid.files;

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

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

/**
 * @author Ramius
 *
 */
public final class FileManager 
{
  /**
   * 
   * @param context
   * @param fileName
   * @param data
   */
  public static void write(final Context context, String fileName, String data)
  {
    FileOutputStream fOut = null;
        OutputStreamWriter osw = null;
 
        try
        { 
           fOut = context.openFileOutput(fileName, Context.MODE_APPEND);       
            osw = new OutputStreamWriter(fOut); 
            osw.write(data); 
            osw.flush(); 
           //popup surgissant pour le rsultat
            Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show(); 
        } 
        catch (Exception e) 
        {       
          Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show(); 
        } 
        finally 
        { 
          try 
           { 
            osw.close(); 
            fOut.close(); 
           } 
          catch (IOException e) 
          { 
            Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show(); 
          } 
        } 
  }
  
  /**
   * 
   * @param context
   * @param fileName
   * @return
   */
  public static String read(final Context context, String fileName)
  {
    FileInputStream fIn = null; 
        InputStreamReader isr = null; 
 
        char[] inputBuffer = new char[255]; 
        String data = null; 
 
        try
        { 
          fIn = context.openFileInput(fileName);       
          isr = new InputStreamReader(fIn); 
          isr.read(inputBuffer); 
          data = new String(inputBuffer);
          
          //affiche le contenu de mon fichier dans un popup surgissant
          Toast.makeText(context, " "+data,Toast.LENGTH_SHORT).show(); 
        } 
        catch (Exception e) 
        {       
          Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show(); 
        } 
        finally 
        { 
        try 
        { 
          isr.close(); 
          fIn.close(); 
        } 
        catch (IOException e) 
        { 
          Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show(); 
        } 
        }
        return data; 
  }
  

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.