text reader writer as a class. Write anywhere in any folder.
you can set the file to overwrite and or append, not sure if overWriteFirst is necessary. But everything works.
just make sure to call close()
after writing, other wise move the close method into the write methods.
setLocation();
this will populate the folderPath, fileName and ext of the file.
there is then a counter currently set to a maximum of 100 files. This is to increment up if a file with the matching filename is found based on the starting params you have chosen.
Overwrite will simply overWrite any file of the same name and keep saving to that same location.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import android.app.Activity;
import android.content.Context;
import android.os.Environment;
FileOutput f;
String s1 ,s2 = "iuiugifuy";
public void setup(){
size(600,600);
f = new FileOutput(this);
f.setLocation("notes/test.txt");
f.checkLocation();
};
public void draw(){
background(255);
if(f.fileContent!=null){
fill(0);
text(f.fileContent,20,20);
}else{
fill(0);
text("no file",20,20);
}
};
void mousePressed(){
f.writeLn(s2+hour()+":"+minute()+":"+second());
f.close();
f.loadStrings();
};
class FileOutput {
public PApplet applet;
public FileWriter output;
public boolean save, onMouseUp, mdown, debug, append = true, appendFile, match, append2, overWrite = true, overWriteOnce = true,writeOnce;
public int counter=-1, counter2;
public File file, file2;
public File[] SDcards ;
public String location, filePath, folderPath = "";
String text = "oioijsofoivnsoindv", absolutePath, ext, fileName,fileContent = "";
Permission p;
Activity activity;
Context context;
FileOutput() {
};
FileOutput(boolean a, PApplet app) {
applet = app;
p = new Permission(app,"WRITE_EXTERNAL_STORAGE");
overWrite = true;
appendFile = true;
};
FileOutput(PApplet app) {
applet = app;
p = new Permission(app,"WRITE_EXTERNAL_STORAGE");
};
//currently unused
FileOutput(String location, PApplet app) {
applet = app;
p = new Permission(app,"WRITE_EXTERNAL_STORAGE");
setLocation(location);
};
FileOutput(PApplet app, String location) {
applet = app;
p = new Permission(app,"WRITE_EXTERNAL_STORAGE");
setLocation(location);
};
public void getAndroidInfo() {
activity = applet.getActivity();
context = activity.getApplicationContext();
absolutePath = new String(Environment.getExternalStorageDirectory().getAbsolutePath());
};
void loadStrings() {
loadFile(context);
};
String loadFile(Context context) {
FileInputStream fis = null;
try {
fis = new FileInputStream (new File(file2.getAbsolutePath()));
InputStreamReader isr = new InputStreamReader(fis);
// READ STRING OF UNKNOWN LENGTH
StringBuilder sb = new StringBuilder();
char[] inputBuffer = new char[2048];
int l;
// FILL BUFFER WITH DATA
while ((l = isr.read(inputBuffer)) != -1) {
sb.append(inputBuffer, 0, l);
println("write data", inputBuffer, 0, l);
}
// CONVERT BYTES TO STRING
fileContent = sb.toString();
fis.close();
}
catch (Exception e) {
println("cannot fetch file", e);
}
finally {
if (fis != null) {
fis = null;
}
}
return fileContent;
};
void open() {
};
void close() {
try {
output.flush();
output.close();
}
catch(IOException e) {
}
};
void write(String s) {
if(!overWrite)checkLocation();
try {
output = new FileWriter(file2, append);
}
catch(IOException e) {
}
printWrite(s);
};
void writeLn(String s) {
if(!overWrite)checkLocation();
try {
output = new FileWriter(file2, append);
}
catch(IOException e) {
}
printWriteLn(s);
};
void write(String []s) {
if(!overWrite)checkLocation();
try {
output = new FileWriter(file2, append);
}
catch(IOException e) {
}
printWrite(s);
};
void writeLn(String []s) {
if(!overWrite)checkLocation();
try {
output = new FileWriter(file2, append);
}
catch(IOException e) {
}
printWriteLn(s);
};
void printWrite(String s) {
if(!overWrite)checkLocation();
try {
output.append(s);
}
catch(IOException e) {
}
};
void printWriteLn(String s) {
if(!overWrite)checkLocation();
try {
output.append(s);
output.append("\n");
}
catch(IOException e) {
}
};
void printWrite(String []s) {
if(!overWrite)checkLocation();
try {
for(int i=0;i<s.length;i++){
output.append(s[i]);
}
}
catch(IOException e) {
}
};
void printWriteLn(String []s) {
if(!overWrite)checkLocation();
try {
for(int i=0;i<s.length;i++){
output.append(s[i]);
output.append("\n");
}
}
catch(IOException e) {
}
};
void checkLocation() {
boolean k = false;
try {
//
file = new File(absolutePath, folderPath);
if (!file.exists()&&counter==-1) {
file.mkdirs();
file2 = new File(file, "/"+fileName+"."+ext);
PApplet.println("checking file1", file2);
if (file2.exists()) {
counter ++;
}
} else k = true;
if (overWrite&&writeOnce){
k=false;
}else if(overWrite&&!writeOnce){
writeOnce = true;
k = true;
}
boolean k1 = false;
while (k&&counter<100&&!k1) {
try {
file2 = new File(absolutePath+"/"+folderPath+"/"+fileName+counter+"."+ext);
if (file2.exists()) {
counter++;
} else {
PApplet.println("checking file2", file2);
//output = new FileWriter(file2, append);
k = false;
k1 = true;
break;
}
}
catch (Exception e) {
PApplet.println("Error while saving file: " + e);
}
}
if (overWriteOnce)overWriteOnce = false;
if (k1) {
//PApplet.println("file",file);
//PApplet.println("Fname",folderName);
//PApplet.println("fileName",fileName);
//PApplet.println("counter",counter);
//PApplet.println("ext",ext);
if (file2==null&&counter<0)file2 = new File(absolutePath+"/"+folderPath+"/"+fileName+"."+ext);
else if(file2==null)file2 = new File(absolutePath+"/"+folderPath+"/"+fileName+"."+ext);
if (counter==-1) {
//img.save(file+"/"+fileName+counter+"."+ext);
output = new FileWriter(file2, append);
//use write or append
}
//PApplet.println("File saved successfully.");
}
println("write file", folderPath, fileName,counter, ext);
}
catch (IOException e) {
e.printStackTrace();
}
};
void setLocation(String s) {
getAndroidInfo();
location = s;
folderPath = s.substring(0, s.lastIndexOf("/"));
fileName = s.replace(folderPath+"/", "");
getExt(fileName);
PApplet.println("Fname", folderPath);
PApplet.println("fileName", fileName);
PApplet.println("counter", counter);
PApplet.println("ext", ext);
}
void getExt(String location) {
int count = 0;
fileName = location.substring(0, location.indexOf("."));
ext = location.replace(fileName, "");
ext = ext.replace(".", "");
ext = ext.replace(fileName, "");
};
};
public class Permission{
PApplet parent;
String p;
public Permission(PApplet pParent,String permissionName) {
p = permissionName;
parent = pParent;
parent.requestPermission("android.permission."+permissionName, "onPermissionResult", this);
println(permissionName);
};
public void onPermissionResult(boolean granted) {
if (!granted) {
PApplet.println("User did not grant camera permission.",p,"is disabled.");
}
};
};