Android Open Source - hacktoolkit-android_lib File Utils






From Project

Back to project page hacktoolkit-android_lib.

License

The source code is released under:

MIT License

If you think the Android project hacktoolkit-android_lib 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 com.hacktoolkit.android.utils;
/*from w ww  . j  a  v a 2 s .c o m*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;

// LOL, I wrote my own FileUtils, but there is an existing one.
// Somehow I thought I was supposed to use android.os.FileUtils, which doesn't exist
// http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html
// http://commons.apache.org/proper/commons-io/download_io.cgi

public class FileUtils {
  public static ArrayList<String> readLines(Activity activity, String filename) throws IOException {
    Context context = activity.getApplicationContext();
    ArrayList<String> lines = new ArrayList<String>();
    try {
      FileInputStream fin = context.openFileInput(filename);
      if (fin != null) {
        InputStreamReader inputStreamReader = new InputStreamReader(fin);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line;
        while (( line = bufferedReader.readLine() ) != null) {
          lines.add(line);
        }
        fin.close();
      }
    } catch (FileNotFoundException fnfe) {
      fnfe.printStackTrace();
    }
    return lines;
  }
  
  public static int writeLines(Activity activity, String filename, ArrayList<String> lines) throws IOException {
    int linesWritten = 0;
    Context context = activity.getApplicationContext();
    try {
      FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
      for (String line : lines) {
        // add terminal character so that it doesn't get written as one line
        fos.write((line + "\n").getBytes());
        ++linesWritten;
      }
      fos.close();
    } catch (FileNotFoundException fnfe) {
      fnfe.printStackTrace();
    }
    return linesWritten;
  }
}




Java Source Code List

com.hacktoolkit.android.activities.HTKSplashScreenActivity.java
com.hacktoolkit.android.adapters.HTKContactsAdapter.java
com.hacktoolkit.android.api_helpers.FacebookHelper.java
com.hacktoolkit.android.api_helpers.HTKCallback.java
com.hacktoolkit.android.api_helpers.ParseHelper.java
com.hacktoolkit.android.constants.GeoConstants.java
com.hacktoolkit.android.constants.StringConstants.java
com.hacktoolkit.android.constants.TimeConstants.java
com.hacktoolkit.android.constants.UnitConstants.java
com.hacktoolkit.android.fragments.ContactsFragment.java
com.hacktoolkit.android.maps.MapUtils.java
com.hacktoolkit.android.maps.Mapper.java
com.hacktoolkit.android.models.HTKContact.java
com.hacktoolkit.android.user.HTKUser.java
com.hacktoolkit.android.utils.BitmapUtils.java
com.hacktoolkit.android.utils.ContactsUtils.java
com.hacktoolkit.android.utils.FileUtils.java
com.hacktoolkit.android.utils.HTKUtils.java
com.hacktoolkit.android.utils.SystemUiHiderBase.java
com.hacktoolkit.android.utils.SystemUiHiderHoneycomb.java
com.hacktoolkit.android.utils.SystemUiHider.java