Menu
Topics Index
...
`


Input/Output: Exploring java.io > Byte Streams >
Siva Nookala - 07 Apr 2016
DataInputStream is an input stream that contains methods for reading the java standards data types.
DataOutputStream is an output stream that contains methods for methods for writing the java standard data types.


DataInputStream :

DataInputStream extends FilterInputStream, which extends InputStream, and it implements the DataInput interface.

Constructor: DataInputStream(InputStream is)

DataInputStream Methods :

  • Double readDouble() throws IOException.
  • Boolean readBoolean() throws IOException .
  • Int readInt() throws IOException.


DataOutputStream:

DataOutputStream extends filterOutputStream, which extends outputstream.

Constructor: dataoutputstream(outputstream is)

DataOutputStream Methods :

  • final void writeDouble(double value) throws IOException.
  • final void writeBoolean(Boolean value) throws IOException.
  • final void writeInt(int value) throws IOException.
Count
import java.io.*;

class Count
{
    public static void main(String arg[])
    {
        DataInputStream dis= new DataInputStream(System.in);
                System.out.println("Enter the file name");
                String str=dis.readLine();
                FileInputStream fis=new FileInputStream(str);
                int k,nc=0,nw=1,nl=1;
                while((k=fis.read())!=-1)
                {
                    char ch=(char)k;
                    if(ch==' '||ch=='\t'||ch=='\n') //LINE A
                        nw++;
                    if(ch=='\n')
                        nl++;
                    nc++;
                }
                System.out.println("no:of characters="+nc);
                System.out.println("no:of words="+nw);
                System.out.println("no:of lines="+nl);
                fis.close();
    
    }
}
OUTPUT

Enter the file name:
Sample.java
No:of characters=110
No:of words=21
no:of lines=8

DESCRIPTION

In this program , we demonstrate that counting the lines , words , characters of a file. Here in this program we take a sample.java file to count.

THINGS TO TRY
if u don’t place a null character in LINE A i.e.,
if(ch==' '||ch=='\t'||ch=='\n')
it shows an error called
empty character literal (or) unclosed character literal
check out this.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App