High-level input and output streams communicate with low-level streams rather than with I/O devices. You can use them for high-level input and output.
Most of Java's high-level input and output classes inherit attributes from the
FilterInputStream and FilterOutputStream superclasses. In turn, these classes inherit from the abstract InputStream and OutputStream classes.
Suppose you are using a
DataInputStream constructor for one of these classes. You need to pass an InputStream to the constructor as a parameter.
DataInputStream(InputStream objectName)
You can use any class that inherits from the
InputStream class as an input source for a high-level stream. For example, you can use a FileInputStream object that you have already created, or use input from a socket or pipe.
When a high-level stream object, such as an instance of the
The
DataInputStream class, receives byte input from a low-level stream, it processes the bytes and converts them into the appropriate datatype. The
DataInputStream class contains read methods that convert bytes into all the primitive datatypes, as well as into UTF strings. For example, the readInt method reads the next four bytes and converts them into an int. For the methods to work correctly, these four bytes must represent an int. You need to make sure that the data is read in the same order in which it is written to a stream.
To close a
If you need to close a chain of stream objects, you do so in reverse order so that the object that was created first is the last one to close.
This prevents you from closing an
DataInputStream object, you use the class's close method.If you need to close a chain of stream objects, you do so in reverse order so that the object that was created first is the last one to close.
This prevents you from closing an
InputStream before you close the high-level stream that uses it as an input source.
For example, the code sample that reads sales data from a file uses the
close method to close an instance of the DataInputStream object. for (int i = 0; i < descs.length; i ++) {
myData = new SalesData (descs[i], amounts[i], values[i]);
writeSalesData ( myData ) ;
}
out.close() ;
// Prepare to read it in
in = new DataInputStream(new FileInputStream(fruitfile)) ;
for (int i=0; i<6; i++) {
myData = readSalesData () ;
System.out.println("You sold " +
myData.desc + " at $" +
myData.value + " each. Amount was " + myData.amount) ;
}
in.close() ;
}
The subclasses of the
FilterOutputStream class includeDataOutputStreamBufferedOutputStreamPrintStream
DataOutputStream- You use the
DataOutputStreamto write data to a stream by passing anOutputStreamto aDataOutputStreamobject as a parameter when you create the object. BufferedOutputStream- You use the
BufferedOutputStreamto write data to a buffer. This in turn writes it to the underlying stream. PrintStream- A
PrintStreamallows other output streams to conveniently print data of various formats. This class never throws anIOException, unlike other output streams.
The methods of the
DataOutputStream class process data, such as characters, integers, and UTF strings, convert it to bytes, and write it to the stream.File f = new File (myFileName);
if(f.exists() && f.isFile() && f.canWrite()) {
try {
FileOutputStream fostream =
new FileOutputStream(f);
DataOutputStream dostream =
new DataOutputStream(fostream);
dostream.writeUTF("Some UTF data");
dostream.close();
fostream.close();
}
catch (IOException e) {
}
}
Supplement
Selecting the link title opens the resource in a new browser window.
View the
DataInputStream and DataOutputStream methods.
Consider the code that creates a
FileOutputStream object named fostream, and a DataOutputStream object named dostream.File f = new File (myFileName);
if(f.exists() && f.isFile() && f.canWrite()) {
try {
FileOutputStream fostream =
new FileOutputStream(f);
DataOutputStream dostream =
new DataOutputStream(fostream);
dostream.writeUTF("Some UTF data");
dostream.close();
fostream.close();
}
catch (IOException e) {
}
}
In doing so, the code writes the
DataOutputStream to the FileOutputStream. - fostream.
It passes a string as a parameter to the
writeUTF method, which writes it to the output stream.
Finally, it closes the two streams in the correct order. This way, the one that was created last is closed first.
File f = new File (myFileName);
if(f.exists() && f.isFile() && f.canWrite()) {
try {
FileOutputStream fostream =
new FileOutputStream(f);
DataOutputStream dostream =
new DataOutputStream(fostream);
dostream.writeUTF("Some UTF data");
dostream.close();
fostream.close();
}
catch (IOException e) {
}
}







4 comments:
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
microsoft azure training in bangalore
rpa training in bangalore
best rpa training in bangalore
Fantastic work! This is the type of information that should follow collective approximately the web. Embarrassment captivating position Google for not positioning this transmit higher! Enlarge taking place greater than and visit my web situate
python Course in Pune
python Course institute in Chennai
python Training institute in Bangalore
I am upbeat to locate your recognized method for composing the post. Presently you make it simple for me to comprehend and execute the idea. Much obliged to you for the post. Wondering where to go in 2020? Things to do has ranked as the best include a remote, idyllic island and the design capital.
High-Level Streams ~ Java Tutorials >>>>> Download Now
>>>>> Download Full
High-Level Streams ~ Java Tutorials >>>>> Download LINK
>>>>> Download Now
High-Level Streams ~ Java Tutorials >>>>> Download Full
>>>>> Download LINK
Post a Comment