Write to File in Java 10 Using FileOutputStream

By | April 15, 2018

In this java tutorial we will learn about “Write to File in Java” with FileOutputStream. FileOutputStream is a bytes stream class that’s used to handle raw binary data. To write the data to file, first you must convert the data into bytes and then save it to file. Let’s see below for full java example.

Write to File in Java 10

Note that in this tutorial i am using java 10 and i hope this will help you to solve your problem.

package com.cimblo.io;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @since 15/04/2018
 */
public class WriteToFileJava10FileOutputStream {

    public static void main( String[] args ){

        var write = new WriteToFileJava10FileOutputStream();

//example 1
        write.WriteToFileJava10FileOutputStreamExample1();
//example 2
        write.WriteToFileJava10FileOutputStreamExample2();

    }

    public void WriteToFileJava10FileOutputStreamExample1(){

        FileOutputStream fop = null;
        var content = "Promo Tiket Murah Traveloka (2018,2019,2020), Promo Diskon Tiket Kereta dan Pesawat Traveloka";

        try {
            var file = new File("D:/promoTiketMurahTraveloka.txt");

            fop = new FileOutputStream(file);

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            // get the content in bytes
            var contentInBytes = content.getBytes();

            fop.write(contentInBytes);
            fop.flush();
            fop.close();

            System.out.println("Write to file with java 10 is SOLVED!");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fop != null) {
                    fop.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public void WriteToFileJava10FileOutputStreamExample2(){
        var file = new File("D:/promoDiskonTiketMurahTraveloka.txt");
        var content = "Promo Tiket Murah Traveloka, Promo Diskon Tiket Kereta dan Pesawat Traveloka (2018,2019,2020)";

        try (var fop = new FileOutputStream(file)) {

            // if file doesn't exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            // get the content in bytes
            var contentInBytes = content.getBytes();

            fop.write(contentInBytes);
            fop.flush();
            fop.close();

            System.out.println("Write to file with java 10 is SOLVED!");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

What is different example 1 and example 2? In example 2 i’m using new “try resource close” method to handle file easily.

References

  1. http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileOutputStream.html
  2. Read file in java using FileInputStream

One thought on “Write to File in Java 10 Using FileOutputStream

  1. Pingback: Read File in Java 10 Using FileInputStream ~ Cimblo.com

Leave a Reply