Skip to content

Instantly share code, notes, and snippets.

@guidolodetti
Last active May 17, 2017 21:24
Show Gist options
  • Save guidolodetti/cb9c16a9df901a312e545cfe2b7d73d8 to your computer and use it in GitHub Desktop.
Save guidolodetti/cb9c16a9df901a312e545cfe2b7d73d8 to your computer and use it in GitHub Desktop.
Simple file reader for Android. Just add this file to your project!
// PUT YOUR PACKAGE HERE
// package com.mycompany.myproduct;
import android.content.Context;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* Created by Guido Lodetti on 17/05/2017.
*
* This static class allows you to read from a text file in the following formats: String, JSONObject, JSONArray.
* If an error occurs, null is returned.
*
* Here are the available methods:
*
* String ->
* SimpleFileReader.stringFromFile(Context context, String path)
* SimpleFileReader.stringFromAsset(Context context, String name)
*
* JSONObject ->
* SimpleFileReader.jsonObjectFromFile(Context context, String path)
* SimpleFileReader.jsonObjectFromAsset(Context context, String name)
*
* JSONArray ->
* SimpleFileReader.jsonArrayFromFile(Context context, String path)
* SimpleFileReader.jsonArrayFromAsset(Context context, String name)
*
*/
public class SimpleFileReader {
private static String stringFromInputStream(InputStream is) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String contents = "";
String line;
while ((line = reader.readLine()) != null) {
contents += line;
}
return contents;
} catch (Exception ex) {}
return null;
}
public static String stringFromAsset(Context context, String name) {
try {
return stringFromInputStream(context.getAssets().open(name));
} catch (Exception ex) {}
return null;
}
public static String stringFromFile(Context context, String path) {
try {
return stringFromInputStream(context.openFileInput(path));
} catch (Exception ex) {}
return null;
}
private static JSONObject jsonObjectFromString(String string) {
if (string == null || string.length() == 0) return null;
try {
return new JSONObject(string);
} catch (Exception ex) {}
return null;
}
public static JSONObject jsonObjectFromFile(Context context, String path) {
return jsonObjectFromString(stringFromFile(context, path));
}
public static JSONObject jsonObjectFromAsset(Context context, String name) {
return jsonObjectFromString(stringFromAsset(context, name));
}
private static JSONArray jsonArrayFromString(String string) {
if (string == null || string.length() == 0) return null;
try {
return new JSONArray(string);
} catch (Exception ex) {}
return null;
}
public static JSONArray jsonArrayFromFile(Context context, String path) {
return jsonArrayFromString(stringFromFile(context, path));
}
public static JSONArray jsonArrayFromAsset(Context context, String name) {
return jsonArrayFromString(stringFromAsset(context, name));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment