Java DataOutputStream Class · Private Archive
Java DataOutputStream Class
A data output stream allows an application to write Java primitive data types to the underlying output stream in a machine-independent manner.
DataOutputStream out = new DataOutputStream(OutputStream out);
The following constructor is used to create a data output stream object.
| No. | Method Description |
|---|---|
| 1 | public final void write(byte[] w, int off, int len)throws IOException |
Writes len bytes from the specified byte array starting at offset off starting at len bytes into this byte array output stream. |
|
| 2 | Public final int write(byte [] b)throws IOException |
| Writes the specified byte to this byte array output stream. | |
| 3 |
|
Example
Reads bytes from the input stream, returning the two bytes from the input stream as the corresponding primitive data type return value. | | 4 | **public String readLine() throws IOException** Reads the next line of text from the input stream. |
The following example demonstrates the use of DataInputStream and DataOutputStream; it reads 5 lines from the text file test.txt, converts them to uppercase letters, and finally saves them in another file test1.txt.
runoob1
runoob2
runoob3
runoob4
runoob5
Example
import
java
.
io
.*;
public
class
Test
{
public
static
void
main
(
String
args
[
]
)
throws
IOException
{
DataInputStream
in
=
new
DataInputStream
(
new
FileInputStream
(
"
test.txt
"
)
)
;
DataOutputStream
out
=
new
DataOutputStream
(
new
FileOutputStream
(
"
test1.txt
"
)
)
;
BufferedReader
d
=
new
BufferedReader
(
new
InputStreamReader
(
in
)
)
;
String
count
;
while
(
(
count
=
d
.
readLine
(
)
)
!=
null
)
{
String
u
=
count
.
toUpperCase
(
)
;
System
.
out
.
println
(
u
)
;
out
.
writeBytes
(
u
"
,
"
)
;
}
d
.
close
(
)
;
out
.
close
(
)
;
}
}
The compilation and execution results of the above example are as follows:
RUNOOB1
RUNOOB2
RUNOOB3
RUNOOB4
RUNOOB5
Java Stream