How to write a byte[] into a file?

Here is a simple program that will write a byte[] into a file with converting File To Bytes

  • First Example with FileInputStream and FileOutputStream:
package com.nourit.javaIO;

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

public class BytesToFileExample {

  public static void main(String[] args) throws IOException {
    // specify your bytes here.
    File file = new File("D:/tmp/nourit.txt");
    byte[] bytesFile = new byte[(int) file.length()];
    // read file into bytes[]
    FileInputStream fileInputStream = new FileInputStream(file);
    fileInputStream.read(bytesFile);
    // write Bytes To File
    FileOutputStream fileOuputStream = new FileOutputStream("D:/tmp/NOUR-IT-Destination.txt");
    fileOuputStream.write(bytesFile);
    if (fileOuputStream != null) {
      fileOuputStream.close();
    }
    if (fileInputStream != null) {
      fileInputStream.close();
    }
    System.out.println("Operation successful!");
  }
}
  • Second example with java.nio.file.Files:
package com.nourit.javaIO;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class BytesToFileExample1 {

  public static void main(String[] args) throws IOException {
    // specify your bytes here.
    File file = new File("D:/tmp/nourit.txt");
    byte[] bytesFile = new byte[(int) file.length()];
    // read file into bytes[]
    FileInputStream fileInputStream = new FileInputStream(file);
    fileInputStream.read(bytesFile);
    // write Bytes To File
    Path path = Paths.get("D:/tmp/NOUR-IT-Destination.txt");
    Files.write(path, bytesFile);

    if (fileInputStream != null) {
      fileInputStream.close();
    }
    System.out.println("Operation successful!");
  }
}
  • Third example with FileUtils.readFileToByteArray:
package com.nourit.javaIO;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.commons.io.FileUtils;

public class BytesToFileExample2 {

  public static void main(String[] args) throws IOException {
    // specify your bytes here.
        File file = new File("D:/tmp/nourit.txt");
        byte[] bytesFile = FileUtils.readFileToByteArray(file); 
        // read file into bytes[]
        FileInputStream fileInputStream = new FileInputStream(file);
        fileInputStream.read(bytesFile);
        // write Bytes To File
        Path path = Paths.get("D:/tmp/NOUR-IT-Destination.txt");
        Files.write(path, bytesFile);

        if (fileInputStream != null) {
          fileInputStream.close();
        }
        System.out.println("Operation successful!");
  }
}

Output:

Operation successful!

 

NOUR-IT

IT Training Center for Java and Web technology. If you enjoy and like our tutorials and believe in our mission, Please Subscribe us on Youtube and follow us on Twitter, or befriend us on Facebook or Google Plus.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*