Given a file and we have to write text and print the total number of written bytes using DataOutputSteam using Java program.
There is a file named "IncludeHelp.txt" which is stored at my system in "E:" drive under "JAVA" folder (you can choose your path), and in this file we will write some text and print the total number of written bytes.
Example:
Text to write in file: "IncludeHelp is for computer science students."
Output: Total 45 bytes are written to stream.
First of all we will create an object of FileOutputStream by passing the file’s path and then we will create an object of DataOutputStream by passing object of FileOutputStream.
Then, we will write the text into file using writeBytes() method of DataOutputStream and finally we will get the size of the file by using size() method of DataOutputStream class.
In this program,
- objFOS is the object of FileOutputStream class
- objDOS is the object of DataOutputStream class
-
Consider the program
Output