Java — How to write to a File | Code Factory

Java write to File using BufferedWriter

package com.example.java.programming.file;import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* @author code.factory
*
*/
public class BufferedWriterExample {
public static void main(String... args) {
Path filePath = Paths.get("test.txt");
Charset charset = StandardCharsets.UTF_8;
// Instantiate a BufferedWriter
try (BufferedWriter writer = Files.newBufferedWriter(filePath, charset)) {
// Write data
writer.write("Hello, World!");
writer.newLine();
writer.write("Learn how to write data to a File efficiently using BufferedWriter");
writer.newLine();
} catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
}
}

Java write to File using PrintWriter

package com.example.java.programming.file;import java.io.BufferedWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* @author code.factory
*
*/
public class PrintWriterExample {
public static void main(String... args) {
Path filePath = Paths.get("test.txt");
Charset charset = StandardCharsets.UTF_8;
// Instantiate a new FileWriter by wrapping a buffered writer for improved performance
try (BufferedWriter bufferedWriter = Files.newBufferedWriter(filePath, charset)) {
PrintWriter printWriter = new PrintWriter(bufferedWriter);
// Write formatted data using print writer
printWriter.println("Hello, World!");
printWriter.printf("The value of PI is %f\n", Math.PI);
printWriter.println(123456L);
printWriter.println(true);
} catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
}
}

Java write to File using FileWriter

package com.example.java.programming.file;import java.io.FileWriter;
import java.io.IOException;
/**
* @author code.factory
*
*/
public class FileWriterExample {
public static void main(String... args) {
// Instantiate a new FileWriter
try (FileWriter fileWriter = new FileWriter("test.txt")) {
// Write data
fileWriter.write("Hello, World!\n");
fileWriter.write("Learn how to write data to a File using FileWriter\n");
} catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
}
}

Java write to File using BufferedOutputStream

package com.example.java.programming.file;import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* @author code.factory
*
*/
public class BufferedOutputStreamExample {
public static void main(String... args) {
String data = "This example shows how to Write data to a File using BufferedOutputStream";
// Create a BufferedOutputStream by wrapping a FileOutputStream
try (OutputStream outputStream = Files.newOutputStream(Paths.get("test.txt"));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream)) {
// Write binary data to a file
bufferedOutputStream.write(data.getBytes());
} catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
}
}

Java write to File using FileOutputStream

package com.example.java.programming.file;import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author code.factory
*
*/
public class FileOutputStreamExample {
public static void main(String... args) {
String data = "This example shows how to Write data to a File using FileOutputStream";
// Instantiate a new FileOutputStream
try (FileOutputStream outputStream = new FileOutputStream("test.txt")) {
// Write data in the form of bytes
outputStream.write(data.getBytes());
} catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
}
}

Java write to File using DataOutputStream

package com.example.java.programming.file;import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* @author code.factory
*
*/
public class DataOutputStreamExample {
public static void main(String... args) {
// Create a DataOutputStream by wrapping a BufferedOutputStream for improved
// performance
try (OutputStream outputStream = Files.newOutputStream(Paths.get("test.txt"));
DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(outputStream))) {
// Write primitive Java data types
dataOutputStream.writeUTF("Hello!");
dataOutputStream.writeLong(Long.MAX_VALUE);
dataOutputStream.writeDouble(3.14);
dataOutputStream.writeBoolean(true);
} catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
}
}

Java write to File using RandomAccessFile

package com.example.java.programming.file;import java.io.IOException;
import java.io.RandomAccessFile;
/**
* @author code.factory
*
*/
public class RandomAccessFileExample {
public static void main(String... args) {
try (RandomAccessFile randomAccessFile = new RandomAccessFile("test.txt", "rw")) {
randomAccessFile.seek(10);
randomAccessFile.writeChar('-');
randomAccessFile.writeInt(123456);
randomAccessFile.writeChar('-');
} catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
}
}

Java write to File using FileChannel

package com.example.java.programming.file;import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* @author code.factory
*
*/
public class FileChannelExample {
public static void main(String... args) {
String data = "This example shows how to write to a File using FileChannel. \n"
+ "FileChannel can be faster thant standard I/O if you're dealing with large files";
try (RandomAccessFile randomAccessFile = new RandomAccessFile("test.txt", "rw");
FileChannel fileChannel = randomAccessFile.getChannel()) {
// Create a byte buffer with sufficient capacity to accommodate the data.
byte[] byteData = data.getBytes();
ByteBuffer buffer = ByteBuffer.allocate(byteData.length);
// Transfer the content of the byte array to the buffer
buffer.put(byteData);
// Move to the start position to prepare for channel write
buffer.flip();
// Writes a sequence of bytes to the channel from the given buffer.
fileChannel.write(buffer);
} catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
}
}

Java write to File using Java NIO’s Files.Write

package com.example.java.programming.file;import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* @author code.factory
*
*/
public class FilesWriteExample {
public static void main(String... args) throws IOException {
String data = "This example shows how to write a sequence of bytes to a File using Files.write method.";
Path path = Paths.get("test.txt"); Files.write(path, data.getBytes());
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store