also heres a little class I wrote a while back to write files please note it is using a very old version of my fileWriter class as I encountered a bug on my new one.
In any case just copy it to your sketch file in a new tab, and paste the code if you wish to use it.
Summary
class fileOutput {
PrintWriter output;
boolean save, onMouseUp, mdown, debug, append, appendFile, match, append2, overWrite;
int counter, counter2;
File file;
String location, filePath, folderPath = "";
fileOutput() {
appendFile = true;
};
fileOutput(boolean a) {
overWrite = a;
appendFile = a;
};
fileOutput(String location) {
//appendFile = true;
checkLocation(location);
open();
};
fileOutput(String location, boolean m) {
appendFile = m;
checkLocation(location);
open();
//file = dataFile(location);
//filePath = file.getPath();
};
void checkLocation(String location) {
int count = 0;
for (int i=location.length()-1; i>-1; i--) {
char c = location.charAt(i);
if (c=='\\') {
folderPath = location.substring(0, i);
this.location = location.substring(i, location.length());
count ++;
break;
}
}
if (count==0)this.location = location;
String s1 = folderPath.replace("\\", "");
String s2 = this.location.replace("\\", "");
println("checkLocation: " + s1 + "\\" + counter + "\\" + s2);
};
void update(String folder, String file, int counter ) {
//filePath = folder + "\\" + file;
this.folderPath = folder +"\\";
this.location = file;
this.counter = counter;
appendFile = false;
overWrite = true;
};
void saveData() {
if (mdown()) {
checkFile( location, append);
}
if (mdown)
output.println(mouseX + ",+ " + mouseY);
close();
};
void open() {
checkFile(location, append);
};
void write(String s) {
// use this one to append the file created
if (output!=null) {
output.println(s);
println("successful write", s);
output.flush(); // Writes the remaining data to the file
//output.close(); // Finishes the file
} else print("Create Save File");
};
void write(float f) {
output.println(str(f));
};
void write_(String s) {
//use this one to overwrite file
//output.write(s);
output.println(s);
if (output!=null) {
println("successful overwrite", s);
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
} else println("failed", location, folderPath);
};
void write(String []s) {
String s1 = "";
for (int i=0; i<s.length; i++) {
s1 += s[i];
}
if (s1!=null&&output!=null)output.println(s1);
};
void close() {
if (output!=null) {
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
} else println(location, folderPath);
};
boolean onMouseUp() {
boolean k = false;
if (pos()&&!mousePressed&&onMouseUp) {
onMouseUp = false;
} else if (pos()&&mousePressed&&!onMouseUp) {
output.println(counter);
onMouseUp = true;
k = false;
} else if (onMouseUp&&!mousePressed) {
k = true;
onMouseUp = false;
counter ++;
}
return k;
};
boolean mdown() {
boolean k= false;
if (mdown)k = false;
if (mousePressed&&!mdown) {
mdown = true;
k = true;
}
if (!mousePressed)mdown = false;
return k;
};
boolean pos() {
return mouseX>0&&mouseX<width&&mouseY>0&&mouseY<height;
};
void checkFile(String location, boolean append) {
if (appendFile) {
file = dataFile(folderPath + "/" + location);
filePath = file.getPath();
filePath = filePath.replace(location, "");
String[] list = null;
if (debug)println("checkFile");
if (listNames(filePath)!=null&&!match) {
println(filePath);
list = listNames(filePath);
boolean b = false;
for (int j=maxFolderSize; j>-1; j--) {
//println(j);
if (b)break;
counter = j;
for (int i=list.length-1; i>-1; i--) {
int n = int(list[i]);
if (j == n) {
println(j + ", " + list[i]);
counter = j;
b = true;
break;
}
}
}
match = true;
if (!b)counter = -1;
if (counter>=0)counter = counter + 1;
else counter = 0;
//println(counter);
}
//file = dataFile(folderPath + "/" + counter +"/"+ location);
file = dataFile( location);
filePath = file.getPath();
filePath = filePath.replace(location, "");
}
println(" append",appendFile);
//overWrite||
if (output == null) {
if (appendFile) {
if (file==null)file = dataFile(folderPath + "/" + counter + "/" + location);
output = createWriter("/data/" + folderPath + "/" + counter + "/"+ location);
println(" append");
} else {
if (file==null)file = dataFile(folderPath + "/" + location);
output = createWriter("/data/" + folderPath + "/"+ location);
println("not append");
}
//println("new folderpath " + folderPath + counter + "\\" + location);
//
filePath = file.getPath();
filePath = filePath.replace(location, "");
}
if (debug) println(filePath);
try {
FileWriter fw = new FileWriter(file, false);///true = append
BufferedWriter bw = new BufferedWriter(fw);
output = new PrintWriter(bw);
}
catch(IOException ioe) {
System.out.println("Exception ");
ioe.printStackTrace();
println(filePath);
}
};
};
String[] listNames(String dir) {
if (dir==null)return null;
File file = new File(dir);
if (file.isDirectory()) {
String names[] = file.list();
return names;
} else {
// If it's not a directory
return null;
}
};
int totalFiles(String dir) {
File file = new File(dir);
if (file.isDirectory()) {
String names[] = file.list();
return names.length;
} else {
// If it's not a directory
return -1;
}
};
String getFileExtension(File file) {
String fileName = file.getName();
if (fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0)
return fileName.substring(fileName.lastIndexOf(".")+1);
else return null;
};
and here is the sketch code.
import java.io.BufferedWriter;
import java.io.FileWriter;
int maxFolderSize = 1000;
fileOutput output;
boolean mdown = false;
String loc = "positions.txt";
ArrayList<Obj> objects = new ArrayList<Obj>();
int totalObjects = 100;
String[] data;
void setup() {
output = new fileOutput(loc,false);
for(int i=0;i<totalObjects;i++){
float x = random(width);
float y = random(height);
float z = random(200);
Obj o = new Obj(x,y,z,i);
output.write(x+","+y+","+z+","+o.id);
objects.add(o);
}
data = loadStrings("positions.txt");
for(int i=0;i<10;i++){
String s = data[0];
//split the lines to retrieve the variables
//remember the order is x,y,z,id
// so index[0] = x .....
String []vars = s.split(",");
// this bit is not necessary but just shows you interactions you could add
Obj tile = objects.get(parseInt(vars[3]));
println("tile: ",tile.x,tile.y,tile.z);
//find the data
float x = parseFloat(vars[0]);
float y = parseFloat(vars[1]);
float z = parseFloat(vars[2]);
int id = parseInt(vars[3]);
//create new blank obj
Obj tempTile = new Obj();
//populate obj data
tempTile.id = id;
tempTile.x = x;
tempTile.y = y;
tempTile.z = z;
//pass our object into the constructor
Obj1 o = new Obj1(i,tempTile);
// this is the data for the new objects weve created using the info stored in the text file
println("obj1: ",o.tile1.x,o.tile1.y,o.tile1.z);
}
};
void draw() {
point(mouseX, mouseY);
//add condition here before calling
//click anywhere on the canvas to save
//if(mousePressed&&!mdown){
// output.write("hello");
// mdown = true;
//}
if(!mousePressed)mdown = false;
//use output.saveData("parameter i " + "parameter i++".....)
}
class Obj{
int id;
float x,y, z;
Obj(){
};
Obj(float xx,float yy,float zz,int id_){
x = xx;
y = yy;
z = zz;
id = id_;
};
};
class Obj1{
int id;
float x,y, z;
Obj tile1,tile2,tile3;
Obj1(){
};
Obj1(int id_,Obj Tile1){
id = id_;
tile1 = Tile1;
};
Obj1(int id_,Obj Tile1,Obj Tile2,Obj Tile3){
id = id_;
tile1 = Tile1;
tile2 = Tile2;
tile3 = Tile3;
};
};