This installment has Yakov looking at Java Streams Basics.
#13 |
Frederik De Milde commented on 6 Feb 2005
A little bug in your code snippet on Buffering: buff shouldn't be declared twice...
FileInputStream myFile = null;
BufferedInputStream buff =null
try {
myFile = new FileInputStream("abc.dat");
BufferedInputStream buff = new BufferedInputStream(myFile);
boolean eof = false;
while (!eof) {
int byteValue = buff.read();
System.out.print(byteValue + " ");
if (byteValue == -1)
eof = true;
}
} catch (IOException e) {
e.printStackTrace();
} finally{
buff.close();
myFile.close();
}
|
#12 |
Muiris O Cleirigh commented on 29 Mar 2004
There seems to be a problem rendering this article - this part doesn''t make a lot of sense to me ......
The next code fragment writes into the file xyz.dat using the class FileOutputStream:
int somedata[]={56,230,123,43,11,37};
FileOutputStream myFile = null;
try {
myFile = new FileOutputStream("xyz.dat");
for (int i = 0; i
You can use stream chaining (or stream piping) to connect streams - think of connecting two pipes in plumbing.
Let''s modify the example that reads the file abc.dat to introduce the buffering:
FileInputStream myFile = null;
BufferedInputStream buff =null
try {
myFile = new FileInputStream("abc.dat");
BufferedInputStream buff = new BufferedInputStream(myFile);
boolean eof = false;
while (!eof) {
int byteValue = buff.read();
System.out.print(byteValue + " ");
if (byteValue == -1)
eof = true;
}
} catch (IOException e) {
e.printStackTrace();
} finally{
buff.close();
myFile.close();
}
|
#11 |
Excellent article. This has got to be one of the best Stream tutorials out there. I''m also very impressed with your coverage of the StreamTokenizer -- this is an "esoteric" class that few people know and understand how to use effectively. I''m also very impressed with a nice concise run-down of the File class features, such as directory management. Again, excellent article. Keep up the good work!
Greg
|
#10 |
michael turner commented on 7 Jan 2004
This was a good summarization of Java Stream Basics. I have retained sections of the article for Reference. Thank you for presenting the information
|
#9 |
Sorry, the FileReader should have been closed:
import java.io.StreamTokenizer;
import java.io.FileReader;
public class CustomerTokenizer{
public static void main(String args[]){
StreamTokenizer stream =null;
FileReader fr=null;
try{
fr = new FileReader("customers.txt");
stream = new StreamTokenizer(fr );
while (true) {
int token = stream.nextToken();
if (token == StreamTokenizer.TT_EOF)
break;
if (token == StreamTokenizer.TT_WORD) {
System.out.println("Got the string: " +
stream.sval);
}
if (token == StreamTokenizer.TT_NUMBER) {
System.out.println("Got the number: " +
stream.nval);
}
}
}catch (Exception e){
System.out.println("Can''t read Customers.txt: " +
e.toString());
}
finally{
try{
fr.close();
}catch(Exception e){e.printStackTrace();}
}
}
}
|
#8 |
thomas harris commented on 7 Jan 2004
I tried to compile the CustomerTokenizer class and came up with the following error:
File: C:\java\test\CustomerTokenizer.java [line: 33]
Error: cannot resolve symbol
symbol : method close ()
location: class java.io.StreamTokenizer
I checked the api, and the close() method is available via java.io.FileReader so I am not clear on why I got the above error message. Is the compiler looking for the method in the wrong class?
|
#7 |
One of ther readers asked me the questions listed below, and I decided to publish the answers over here as well.
What is the reason for a buffered reader and buffered
writer--what is the need for buffering?
Buffering allows you to read a bunch of bytes from disk into the buffer in one shot, and then the program gets the data from the buffer from the memory byte by byte, int by int, etc. This minimizes the number of times your program has to access the disk.
StreamTokenizer does not have a close method. How do
you close a Stream Tokenizer?
The StreamTokenizer is not a stream itself - it does not need to be closed.
|
#6 |
Andrey Postoyanets commented on 6 Jan 2004
So far the style of this lessons series has been outstanding. Nothing can be more valuable than informative, easy-to-follow examples with a clear explanation.
I also agree that the usage of Java streams and XML manipulation would be a great topic for one of the future articles.
|
#5 |
Matt Campbell commented on 6 Jan 2004
LOL!
|
#4 |
Steve Krenz commented on 6 Jan 2004
What''s VB ;-)
|
#3 |
Matt Campbell commented on 6 Jan 2004
You know, it still isn''t nearly as easy as getting a stream of chars by line or a whole file for parsing as it is in VB. :)
|
#2 |
Steven Krenz commented on 6 Jan 2004
Good article as far as it goes but what would be much more helpful would be a discussion of how readers differ from and are built on streams. What I always find difficult to remember is the best way to read a whole line of data from a stream and not just characters. The connection between java.io streams and org.xml.sax.InputStream is what I suspect most developers are concerned about right now.
|
#1 |
Warawich Sundarabhaka commented on 5 Jan 2004
very good lesson and example.
|