Java Tutorial #1 – How to Write to a Text File

Courtesy Note: If you found this tutorial helpful, please take a few moments to comment below!

The following notable Java built-in classes are used in this tutorial:

1. “BufferedWriter”
2. “FileWriter”


Java Tutorial #1 – How to Write to a Text File

This will be a very simple code example on writing content to a file. This example writes a simple integer to a text file. It creates a file named “numBlinks.txt”, and outputs “100” to the text file.

[sourcecode language=”java” collapse=”false”]
// Author: Jourdan Bul-lalayao
// Purpose: Small program that logs generated data into a text file

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class DataLog {

private int numBlinks;

public DataLog() {
numBlinks = 0;
}

// Function: gen()
// Purpose: Generate data to be logged
public void gen() {
numBlinks++;
}

// Function: log()
// Purpose: Log data into a .txt file
public void log() {

// Make file for # of blinks
File blinkLog = new File("numBlinks.txt");

// Try/catch statement necessary for FileWriter
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(blinkLog));

// If we don’t use String.valueOf, it will output string equivalent of numBlinks.
writer.write(String.valueOf(numBlinks));

// ALWAYS CLOSE THE WRITER, file will be empty if you don’t close it
writer.close();
} catch (IOException e) {
System.out.println("Unable to write to file!");
e.printStackTrace();
}
}

public static void main(String[] args) {

DataLog log = new DataLog();

// Generate data
for (int i = 0; i < 100; i++) {
log.gen();
}

// Log data into txt file
log.log();
}
}
[/sourcecode]

Questions? Feedback? Please leave a comment below!

Leave a Reply

Your email address will not be published. Required fields are marked *