Skip to content

Instantly share code, notes, and snippets.

@johnmelodyme

johnmelodyme/FileHelper.java Secret

Created Mar 11, 2020
Embed
What would you like to do?
package com.ahmadsyahir.brainwave.Helper;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
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;
public class FileHelper {
private final static String fileName = "brainwave_data.txt";
private final static String localpath = Environment.getDataDirectory() + "/Brainwave/"; // getExternalStorageDirectory()
private final static String ext_path = Environment.getExternalStorageDirectory() + "/Brainwave/"; // getExternalStorageDirectory()
private final static String TAG = FileHelper.class.getName();
public static String ReadFile(Context context) {
String line = null;
try {
FileInputStream fileInputStream = new FileInputStream(new File(ext_path + fileName));
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line + System.getProperty("line.separator"));
}
fileInputStream.close();
line = stringBuilder.toString();
bufferedReader.close();
} catch (FileNotFoundException ex) {
Log.d(TAG, ex.getMessage());
} catch (IOException ex) {
Log.d(TAG, ex.getMessage());
}
return line;
}
// TODO extsdcard, sdcard, local:
public static boolean saveToFile(String data) {
try {
new File(ext_path).mkdir();
File file = new File(ext_path + fileName);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(file, true);
fileOutputStream.write((data + System.getProperty("line.separator")).getBytes());
return true;
} catch (FileNotFoundException ex) {
Log.d(TAG, ex.getMessage());
} catch (IOException ex) {
Log.d(TAG, ex.getMessage());
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.