Android Open Source - sami-android-demo File Utils






From Project

Back to project page sami-android-demo.

License

The source code is released under:

Apache License

If you think the Android project sami-android-demo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package sami.utils;
/*from   ww  w  .j a  v  a2s  .co m*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;

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


public class FileUtils {
  public static final String TAG = FileUtils.class.getName();
  
  /**
   * Writes a text file to private app storage
   * @param context
   * @param fileBaseName only the name of the file, no path
   * @param content text inside the file
   * @return
   */
  public static boolean savePrivateFile(Context context, String fileBaseName, String content){
      try {
          FileOutputStream fos = context.openFileOutput(fileBaseName, Context.MODE_PRIVATE);
          Writer out = new OutputStreamWriter(fos);
          out.write(content);
          out.close();
          return true;
      } catch (IOException e) {
          Log.e(TAG, "Error saving file: "+fileBaseName);
          return false;
      }
  }
  
  /**
   * Returns the content of a private file
   * @param context
   * @param fileBaseName filename, without path
   * @return
   */
  public static String readPrivateFile(Context context, String fileBaseName){
    FileInputStream fis;
    try {
      fis = context.openFileInput(fileBaseName);
    } catch (FileNotFoundException e) {
      Log.w(TAG, "File not found: "+fileBaseName);
      return null;
    }
    BufferedReader r = new BufferedReader(new InputStreamReader(fis));
    StringBuilder stringBuilder = new StringBuilder();
    String line;
    try {
      while ((line = r.readLine()) != null) {
            if (stringBuilder.length() > 0) { 
              stringBuilder.append("\n");
            }
              stringBuilder.append(line);
          }
      r.close();
      return stringBuilder.toString();
    } catch (Exception e) {
      Log.w(TAG, "Problem reading file: "+fileBaseName);
      return null;
    }
  }
  
  /**
   * Deletes the file from private folder
   * @param fileBaseName
   * @return
   */
  public static boolean deletePrivateFile(Context context, String fileBaseName){
    File file = new File(context.getFilesDir()+File.separator+fileBaseName); 
    try {
      if(file.exists()){
        file.delete();
      }
    } catch (Exception e) {
      Log.e(TAG, "Error deleting file: "+fileBaseName);
      return false;
    }
    return true;
  }
}




Java Source Code List

io.samsungsami.android.AccountsActivity.java
io.samsungsami.android.AccountsWebClient.java
io.samsungsami.android.AccountsWebView.java
io.samsungsami.android.Credentials.java
io.samsungsami.android.SamiClient.java
io.samsungsami.android.SamiHub.java
io.samsungsami.android.SamiStack.java
io.samsungsami.android.Sami.java
io.samsungsami.android.api.Call.java
io.samsungsami.android.api.Callback.java
io.samsungsami.android.api.Cast.java
io.samsungsami.android.api.Code.java
io.samsungsami.android.api.Task.java
io.samsungsami.androidclient.Config.java
io.samsungsami.androidclient.DevicesActivity.java
io.samsungsami.androidclient.ExpandableAdapter.java
io.samsungsami.androidclient.GenericAdapter.java
io.samsungsami.androidclient.Item.java
io.samsungsami.androidclient.MessagesActivity.java
io.samsungsami.androidclient.RegisterActivity.java
io.samsungsami.androidclient.SamiActivity.java
sami.utils.FileUtils.java