A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/io/fileoutputstream_write_byte_len.htm below:

FileOutputStream write(byte[] b, int off, int len) method

Java - FileOutputStream write(byte[] b, int off, int len) method Description

The Java FileOutputStream write(byte[] b, int off, int len) writes a portion of a byte array to an output file. Useful when you need to write only a portion of a byte array instead of the entire array.

Declaration

Following is the declaration for java.io.FileOutputStream.write(byte[] b, int off, int len) method −

public void write(byte[] b, int off, int len)
Parameters Return Value

This method does not return any value.

Exception

IOException− If an I/O error occurs.

Example - Usage of FileOutputStream write(byte[] b, int off, int len) method

The following example shows the usage of Java FileOutputStream write(byte[] b, int off, int len) method.

FileOutputStreamDemo.java
package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      FileOutputStream fos = null;
      FileInputStream fis = null;
      byte[] b = {65,66,67,68,69};
      int i = 0;
      char c;
      
      try {
         // create new file output stream
         fos = new FileOutputStream("test.txt");
         
         // writes byte to the output stream
         fos.write(b, 2, 3);
         
         // flushes the content to the underlying stream
         fos.flush();
         
         // create new file input stream
         fis = new FileInputStream("test.txt");
         
         // read till the end of the file
         while((i = fis.read())!=-1) {
         
            // convert integer to character
            c = (char)i;
            
            // prints
            System.out.print(c);
         }
         
      } catch(Exception ex) {
         // if an error occurs
         ex.printStackTrace();
      } finally {
         // closes and releases system resources from stream
         if(fos!=null)
            fos.close();
         if(fis!=null)
            fis.close();
      }
   }
}
Output

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

CDE
Example - Writing a Subset of a String

The following example shows the usage of Java FileOutputStream write(byte[] b, int off, int len) method.

FileOutputStreamDemo.java
package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamDemo {
   public static void main(String[] args) {
      String data = "Hello, FileOutputStream!";

      try (FileOutputStream fos = new FileOutputStream("output.txt")) {
         // Convert string to byte array
         byte[] byteData = data.getBytes();

         // Write only "FileOutputStream" (starting from index 7, length 16)
         fos.write(byteData, 7, 16);

         System.out.println("Selected portion written to output.txt.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
Output

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

Selected portion written to output.txt.
The contents of output.txt will be:
FileOutputStream
Explanation Example - Writing a Subset of Binary Data

The following example shows the usage of Java FileOutputStream write(byte[] b, int off, int len) method.

FileOutputStreamDemo.java
package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamDemo {
   public static void main(String[] args) {
      byte[] binaryData = {65, 66, 67, 68, 69, 70, 71, 72, 73, 74}; // ASCII for "ABCDEFGHIJ"

      try (FileOutputStream fos = new FileOutputStream("binary_output.txt")) {
         // Write only "DEFGH" (from index 3, length 5)
         fos.write(binaryData, 3, 5);

         System.out.println("Subset of binary data written to binary_output.txt.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
Output()

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

Subset of binary data written to binary_output.txt.
Explanation

java_io_fileoutputstream.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4