Java IO Tutorial - Java OutputStreamWriter .write (String str, int off, int len)








Syntax

OutputStreamWriter.write(String str, int off, int len) has the following syntax.

public void write(String str,  int off,  int len)  throws IOException

Example

In the following code shows how to use OutputStreamWriter.write(String str, int off, int len) method.

/*w ww . j a  v  a2s.c om*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class Main {

   public static void main(String[] args) {

      String s = "from java2s.com!";

      try {

         OutputStream os = new FileOutputStream("test.txt");
         OutputStreamWriter writer = new OutputStreamWriter(os);

         FileInputStream in = new FileInputStream("test.txt");

         writer.write(s, 0, 5);

         writer.flush();

         for (int i = 0; i < 5; i++) {
            System.out.print((char) in.read());
         }
         writer.close();
         in.close();
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

The code above generates the following result.