Honestly, I feel that using Java 's File API is the only thing worthy of use here.
What I should tell you, though, is that this would require a lot of learning and some practice.
I myself learnt to use the File API pretty recently, so I consider myself worthy enough to write this ‘little’ guide. ![]()
What I write here in this answer assumes that you first learn about the previous topic I mentioned, so you could be following this in a sort of step-by-step manner later on.
More than enough talk. Here’s the real deal:
The first thing I recommend you to do use another IDE (such as VSCode), and write a few Java programs for File I/O. That should help you learn much faster.
java.io classes such as BufferedReader will help you here.
Here is a JavaDoc! Read a few methods’ names to know what you can do with this class, :D!
Here’s a StackOverflow answer to help you start: How to use BufferedReader in Java - Stack Overflow
The idea, is to make a File object (File file = new File("file_name.txt")). This is basically a way to hold a path to a file, not actually make a file!
You would usually check if the file already exists with the .exists() method, else use .createNewFile()!
You should then make a BufferedReader to read the file. However, a BufferedReader cannot do that alone - you need to feed it a FileReader() first (welcome to the OOP mess, :P)
…that’s what BufferedReader reader = new BufferedReader(new FileReader(file)); is.
“Time for some theory!~”
In C - the programming language the world runs on, we have standard library functions (stuff that comes built into every programming language) for File I/O. The way these work, is by loading a file into memory (RAM), and then proceeding to read each character in it. (Note that modern operating systems of course, will still load the file in small chunks for performance.)
You are then supposed to make changes in the file the way you want to. Once you’re done, you “close the file stream”: this tells the OS to change the contents of the data stored on the disk, to whatever you did to its copy in RAM.
I’m sorry if this sounded like tech jargon and wasn’t understood by you… Anyway, this should tell you the very important things it has to:
- Yes, Java has a way to read files byte-by-byte (
FileInputStreamfor bytes!). - Yes,
BufferedReaderis supposed to make your work easier by giving you lines to read, rather than giving you justchars, orbytes. - You have to remember to “close the file stream”!
Now - making objects from File I/O classes in Java can throw exceptions.
You could make a try { } catch(Exception e) { } block to deal with that.
However, there exists a utility for just this stuff: the “try-with-resources” block! The essence of these, is that they .close() the readers etc. automatically. (Though you would be doing that yourself sometimes. Try doing it manually in those cases!)
…you should go read about it :D!
Having gathered all that information, hopefully, this code should make sense!:
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
// The condition in the loop here simply states that `line` should not be `null`.
// However, before we do that, we simply assign the next line in our file to it.
// Operators can be used inside round brackets / parenthesis,
// ..just like the ternary operator can!
// (The `variable = condition? trueVal : falseVal` thing).
for (String line; (line = reader.readLine()) != null;
// Do something like counting the number of lines here!
) { // Loop begins:
println(line);
//...
}
}
Next up - classes for writing to the file:
There really are two primary ways: new BufferedWriter(FileWriter(file)), or, as the Processing reference also shows you: new PrintWriter(file).
The difference, is that PrintWriter gives you built-in functionality for formatting (such as automatically inserting the newline character using its println() method).
BufferedWriter, lets you handle exceptions yourself. PrintWriter ‘eats’ them!
…which one to use?!
BufferedWriter.
Even though it only has only one method that can write strings (which you’ll probably have to read the docs for every time), it is faster.
There’s nothing wrong with PrintWriter either, I guess. But speed is nice for larger files!
The important part!:
Note that though you can do both reading and writing at the same time, we won’t need to do that, yay!
How? Using a StringBuilder! (And not a StringBuffer!)
StringBuilder text = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
for (String line; (line = reader.readLine()) != null;)
builder.append(line);
}
Using deleteCharAt(), it is possible to edit parts of a line, etcetera, easily.
But we work with Processing!
Using this function, you should be able to write just data/filename for your file locations, and it might work. I have never tested this!
Processing also gives you createReader() and createWriter().
You may want to use Andreas Schlegel’s “controlP5” library to make things easier for a UI.
Also, since Java strings are ‘immutable’ (AKA they remain in memory even when no variable stores them), you would want to use a StringBuilder instead of just a String to hold data. If the file data is too long, it’ll cause memory issues.
controlP5 gives probably gives you the text in another thread (there is a callback function for that!), so you could be using a StringBuffer instead. But apparently, that callback gets a String, which is already sorta’ doing things to memory, so… you could just use that string…
…thanks for reading!