Java Tutorial #1 – How to Write to a Text File (Part 1)

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.

// Author: Jourdan Bul-lalayao
// Purpose: Small program that logs generated data into a text file


public class DataLog {

  private int numBlinks;

  public DataLog() {
    numBlinks = 0;

  // Function: gen()
  // Purpose: Generate data to be logged
  public void gen() {

  // 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.

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

  public static void main(String[] args) {

    DataLog log = new DataLog();

    // Generate data
    for (int i = 0; i < 100; i++) {

    // Log data into txt file

Questions? Feedback? Please leave a comment below!

Inside the Computer Hardware

I just wrote my first few programs in the assembly language. It’s really interesting to see how computers execute instructions under-the-hood. I recommend to you computer programmers that you try out a few of your own programs in assembly. It can be a pain, but you’ll understand the basics of what computers are doing behind the scenes! You can download MARS here, which is a MIPS Assembly Language IDE:

Having said that, I probably won’t write assembly again after I graduate. But, who knows.

I’ll post my assembly programs in my “Code” section soon!


I’ve had the motivation to work on my blog the past few days… but I’ve been too preoccupied with school.

That’s where this entry ends.

Glad I was able to take a three minute break by writing this blog entry.


Bonus points to anyone who understands what I wrote!