outputting one line of a text file per bang
I'm trying to build a java external ( and learn java ) which processes a text file and only outputs one line per bang. The problem is I know very little about java and all the tutorials only demonstrate how to load /display an entire file in java.
Can anyone point me in a good direction?
you should check out "FileReader" (for reading text/characters) and "BufferedReader".
the latter has a readLine() method.
post what you have, so people can see, where you're stuck.
a search for "java FileReader examples" or similar, will provide you with some useful example code.
vb
Right now I've got;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Reader {
void readLine(String fileName)
{
java.io.BufferedReader br = null;
try {
BufferedReader in = new BufferedReader(new FileReader("/users/evanlivingston/Desktop/Text.txt"));
String str;
while ((str = in.readLine()) != null) {
System.out.print(str);
}
in.close();
} catch (IOException e) {
}
}
}
This displays a text file in Terminal, though it displays the entire contents of this. My knowledge of java is terribly weak and I don't know how to direct is to print one specific line of text.
ok, first thing you need to do to bring this into max is to add max.jar to the classpath and in your code to properly extend the MaxObject base class:
import com.cycling74.max.*;
public class Reader extends MaxObject {
...
}
if this is new to you, you should read "WritingMaxExternalsInJava.pdf" which is somewhere inside the javadoc folder.
> This displays a text file in Terminal, though it displays the entire contents of this.
actually it displays the text one line after the other, but it happens so fast, that you can't see the individual lines being printed. the while-loop is doing this, by reading a line from the bufferedReader, checking whether the end has been reached, and if not, simply outputting the string to the console.
so, if you want to request every line separately from max, you should change something about the while loop.
probably you want to add a new method to you class, so in max you can send a "bang" (or whatever) message to the object, to retrieve a new line of text. therefore at least the BufferedReader has to be global, so you can access its contents from different places.
hope that helps.
vb