2008 East
DIAMOND SPONSOR:
Data Direct
Frontiers in Data Access: The Coming Wave in Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
Intel
Virtualization – Path to Predictive Enterprise
Green Hills
IT Security in a Hostile World
JBoss / freedom oss
Practical SOA Approach
GOLD SPONSORS:
Software AG
The Art & Science of SOA: How Governance Enables Adoption
PlateSpin
Effective Planning for Virtual Infrastructure Growth
Fujitsu
Automated Business Process Discovery & Virtualization Service
Ceedo
Workspace Virtualization
Click For 2007 West
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
Service Oriented Architecture (SOA) has been discu...
TOP THREE LINKS YOU MUST CLICK ON


Java Streams Basics
This installment has Yakov looking at Java Streams Basics.
Reader Feedback: Page 1 of 1

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();
}

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();
}

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

This was a good summarization of Java Stream Basics. I have retained sections of the article for Reference. Thank you for presenting the information

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();}
}
}
}

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?

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.

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.

LOL!

What''s VB ;-)

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. :)

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.

very good lesson and example.


FEATURED WHITE PAPERS
YOUR FEEDBACK
Roberto Campione wrote: Good article, that helped alot. Out of interest - have you any examples of importing templates using xmlaccess?
Java Consultant wrote: Good Post.... Java Developer...
Java Web Development Company wrote: Hi, Good Post.. Thanks
Chris DiBona wrote: You meant charles dibona not chris.
Suresh Gopalan wrote: Great article. It helped me a lot to understand JAAS and write the EJBClient.
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SYS-CON FEATURED WHITEPAPERS

BREAKING WEBSPHERE NEWS
Cefic, representing the European Chemical Industry, and IBM (NYSE: IBM) developed a state-of-the-art...
Two of the biggest launches in Rich Internet Application history took place in 2007/2008 when Adobe ...
Red Hat CTO Brian Stevens, Citrix CTO Simon Crosby, Egenera CTO Pete Manca, Allen Stewart, Group Man...
IBM announced that Vantage Deluxe World Travel has increased sales and improved business operations ...
Mike Neil is general manager for virtualization strategy in the Windows Server Division at Microsoft...
The AJAX for IBM WebSphere Platform Early Program is an optionally installable product extension for...
Unify announced the expansion of its Composer for Lotus Notes solution through a partnership with CA...
Reminding people of how its backing was the making of Linux, IBM, to no one's surprise, has thrown i...
IBM claims to have created new species of custom-built, industry-standard, Linux-based rack server f...
A standard from OASIS called Web Services for Remote Portlets (WSRP) is used so portlets can be deco...
Genuitec announced the availability of MyEclipse 6.5 Blue Edition; a next-generation ALM and open so...
ZSL announced its Enterprise 2.0 Computing Framework built on IBM WebSphere sMash and Service Orient...
SYS-CON's upcoming '3rd International Virtualization Conference & Expo' faculty includes such distin...
From Application Virtualization to Xen, a round-up of the virtualization themes & topics being discu...
Red Hat is a trusted open source provider. Red Hat offers enterprise customers a long-term plan fo...
IBM announced that businesses are taking advantage of WebSphere Virtual Enterprise to manage softwar...
IBM announced two new advances in the insurance industry - a solution for improving operational effi...
ParAccel announced it has entered into an original equipment manufacturer (OEM) agreement with IBM. ...
Micro Focus announced the availability of SOA Express 8.0. The new version adds support for direct d...
Genuitec announced the availability of the milestone release of MyEclipse 6.5 Blue Edition, a tailor...
IBM announced it has acquired InfoDyne. InfoDyne specializes in high-speed platforms and data feed c...
ADS BY GOOGLE