We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have managed to make a simple file writing program which can append to a file. Now, I want to know how to edit an already existing text file. I want to be able to access a specific line or something and then change or append stuff onto that line. How do you do these kinds of things?
Here is my code:
import java.io.FileWriter;
import java.io.BufferedWriter;
void setup() {
size(1, 1);
try {
FileWriter output = new FileWriter(sketchPath()+"\\strings.txt", true);
output.write("stuff");
output.write(System.getProperty( "line.separator" ));
output.flush();
output.close();
}
catch(IOException e) {
println("It broke!!!");
e.printStackTrace();
}
exit();
}
Answers
loadStrings()?
Do you mean a text editor like in working wirh cursor and keyboard?
Look at gui libraries
@quasarbright -- If you want to write everything in Java using things like
java.io.FileWriter
then you probably want an approach like this:However, I think that @Lord_of_the_Galaxy has the right recommendation. Use
loadStrings()
-- then modify a line in memory -- then usesaveStrings()
:If you have not already, read the "Manipulating Strings" part of the Data Tutorial:
To test, create a sketch with a data file
list.txt
:Here is a sketch that loads the lines, replaces one line, modifies another, and saves the results in a new file (or, if you choose, in the same file). All of these operations (and others, including appending or inserting) are operations on a simple array of strings, not file operations.