[antlr-interest] v3 stream input

Martin Probst mail at martin-probst.com
Sat May 27 06:09:41 PDT 2006


>>> 		CharArrayWriter wr = new CharArrayWriter();
>>> 		BufferedWriter bw = new BufferedWriter(wr);
>>
>> I think you don't need to have a buffered writer around an im memory
>> char array, but then again, I'm not that great in the complex
>> scientific topic that is reading a simple file in Java ;-)
>
> The only other way I know of would require reallocating a char  
> array in
> a loop or converting a Char (using Vector) to a char (as you don't  
> know
> the length of the file). Of course, an understanding of the complex
> scientific topic that is reading a simple file in Java still escapes
> me :)

Well, then I'd guess this is what you want:

InputStreamReader r = ...;
BufferedReader br  = new BufferedReader(r);
char[] buf = new char[4096];
int bytes = 0;
StringBuild sb = new StringBuilder();
while ((bytes = br.read(buf)) != -1) {
   sb.append(buf, 0, bytes);
}
String res = sb.toString(); // or to char array or whatever

Though the file encoding problem stays.

Martin



More information about the antlr-interest mailing list