How to write a Java String value to a file as a sequence of characters using writeChars method of DataOutputStream. - Java File Path IO

Java examples for File Path IO:DataOutputStream

Description

How to write a Java String value to a file as a sequence of characters using writeChars method of DataOutputStream.

Demo Code


import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class Main {
 
  public static void main(String[] args) {
   //from w  ww . j  a  v  a  2s. co  m
    String strFilePath = "C://Folder//WriteStringAsChars.txt";
   
    try
    {
       FileOutputStream fos = new FileOutputStream(strFilePath);
       DataOutputStream dos = new DataOutputStream(fos);
     
       String str = "This string will be written to file as sequence of characters!";
       dos.writeChars(str);
       dos.close();
    }
    catch (IOException e)
    {
      System.out.println("IOException : " + e);
    }
  }
}

Result


Related Tutorials