Java - DataOutputStream writeChars(String value)



Description

The Java DataOuputStream writeChars(String s) method writes a character sequence to the stream.

Declaration

Following is the declaration for java.io.DataOutputStream.writeChars(String s) method −

public final void writeChars(String s)

Parameters

s − a string value to be written to the underlying output stream.

Return Value

This method does not return any value.

Exception

IOException − If an I/O error occurs.

Example 1

The following example shows the usage of Java DataOutputStream writeChars(String value) method. We've created InputStream, DataInputStream, FileOutputStream and DataOutputStream reference. A String is initialized with some text. A FileOutputStream object is created. Then DataOutputStream is initialized with FileOutputStream object created before. We've written the string to stream using writeChars() method. As next step, stream is flushed. As next step, we've created a FileInputStream object based on file written earlier. Then DataInputStream is initialized with FileInputStream object created. Now dataInputStream is iterated to print the contents. Finally we're closing all the streams.

import java.io.IOException;
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class DataOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      InputStream is = null;
      DataInputStream dis = null;
      FileOutputStream fos = null;
      DataOutputStream dos = null;
      String buf = "Hello World!!";
      
      try {
         // create file output stream
         fos = new FileOutputStream("F:\\test.txt");
         
         // create data output stream
         dos = new DataOutputStream(fos);
         
         // write string to the dos
         dos.writeChars(buf);
         
         // force bytes to the underlying stream
         dos.flush();
         
         // create file input stream
         is = new FileInputStream("F:\\test.txt");
         
         // create new data input stream
         dis = new DataInputStream(is);
         
         // read till end of the stream
         while(dis.available()>0) {
         
            // read character
            char c = dis.readChar();
            
            // print
            System.out.print(c);
         }
         
      } catch(Exception e) {
         // if an I/O error occurs
         e.printStackTrace();
      } finally {
         // releases all system resources from the streams
         if(is!=null)
            is.close();
         if(dos!=null)
            is.close();
         if(dis!=null)
            dis.close();
         if(fos!=null)
            fos.close();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Hello World!!

Example 2

The following example shows the usage of Java DataOutputStream writeChars(String value) method. We've created InputStream, DataInputStream, FileOutputStream and DataOutputStream reference. A String is initialized with some text. A FileOutputStream object is created. Then DataOutputStream is initialized with FileOutputStream object created before. As a special case, we're closing the stream before writing any value to check if it supports writing values after closing it.

We've written the string to stream using writeChars() method. As next step, stream is flushed. As next step, we've created a FileInputStream object based on file written earlier. Then DataInputStream is initialized with FileInputStream object created. Now dataInputStream is iterated to print the contents. Finally we're closing all the streams.

import java.io.IOException;
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class DataOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      InputStream is = null;
      DataInputStream dis = null;
      FileOutputStream fos = null;
      DataOutputStream dos = null;
      String buf = "Hello World!!";
      
      try {
         // create file output stream
         fos = new FileOutputStream("F:\\test.txt");
         
         // create data output stream
         dos = new DataOutputStream(fos);

         // close the stream
         dos.close();         
         
         // write string to the dos
         dos.writeChars(buf);
         
         // force bytes to the underlying stream
         dos.flush();
         
         // create file input stream
         is = new FileInputStream("F:\\test.txt");
         
         // create new data input stream
         dis = new DataInputStream(is);
         
         // read till end of the stream
         while(dis.available()>0) {
         
            // read character
            char c = dis.readChar();
            
            // print
            System.out.print(c);
         }
         
      } catch(Exception e) {
         // if an I/O error occurs
         e.printStackTrace();
      } finally {
         // releases all system resources from the streams
         if(is!=null)
            is.close();
         if(dos!=null)
            is.close();
         if(dis!=null)
            dis.close();
         if(fos!=null)
            fos.close();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

java.io.IOException: Stream Closed
	at java.base/java.io.FileOutputStream.write(Native Method)
	at java.base/java.io.FileOutputStream.write(FileOutputStream.java:318)
	at java.base/java.io.DataOutputStream.writeChars(DataOutputStream.java:297)
	at DataOutputStreamDemo.main(DataOutputStreamDemo.java:27)
Exception in thread "main" java.lang.NullPointerException
	at DataOutputStreamDemo.main(DataOutputStreamDemo.java:56)

As underlying stream FileOutputStream is not supporting write to stream after closing it, we get exception in program execution.

java_dataoutputstream.htm
Advertisements