User Tools

Site Tools


Programming Reference/Librarys

Question & Answer

Q&A is closed


Print Writer

The PrintWriter class is a simple way of writing to files in Java io. It can be used in many ways and offers a great deal of methods inside the class. Some of these are quite common methods throughout the language itself. I will go through them step by step, giving examples for each. Let's get started:


Probably the first method learned in Java was the System.out.println() method. Well, this can be applied to the same thing in the PrintWriter class! It uses most all the same core methods as console (Windows CMD, or Mac Terminal) output. Below is an example of a program that will create a file, open the file, and then write a line “Hello World” inside it, then close it. (closing a file is extremely important).

import; // used for importing the io operations
public class OpenWriteCloseFile 
   public static void main(String[] args)
       // for all of io actions, you must use the try/catch block
       try {
       // here, we create a new file for the computer to read
       File f = new File("name of file");
       // here, we create a way to write to that file, the PrintWriter class takes the file you have created
       // and opens it inside the background, so that the program may write to it later on.
       PrintWriter printWriter = new PrintWriter(f);
       // this will take the printwriter and it will write to the file "Hello World!" as seen in the ()       
       printWriter.println("Hello World!");
       // you MUST close the file, if not, the file will not be seen as written to, and will cause much frustration and 
       // anxiety!
       catch (IOException e) {
          //prints out line onto console
          // for better visualization of error


Now, let us move on to the print() method. Like the println() method, it displays text, numbers, and more; however, there is a difference. With the println() method, it will write the text and will add what is called a 'new line' onto the end. It can be commonly explained by writing the text, and hitting the enter key to go down one. This is what the println() method does. (eliminates the need for enter). The print() method just prints whatever you tell it to, and that's that. Let's look at an example, but for this example, instead of using words, we will use numbers.

public class WriteNumbersToFile {
   public static void main(String[] args) {
       // if you're looking at this article, I will assume that you know the many data types
       int    i = 0;
       double d = 10.0;
       float  f = 100f; // the f is required for floating point numbers
       long   l = 1000;
       // here we do the same as above. Creating the file, opening it, and writing to the file
       // we have to use a try/catch block in file io
       try {
           File f = new File("testFile.txt");
           PrintWriter printWriter = new PrintWriter(f);
           // printing i + 1
           // printing 1 + i
           // printing the double
           //printing d + 1
           // printing 1 + d
           //printing f
           //printing f + 1
           //printing 1 + f
           // printing l
           // printing l + 1
           //printing 1 + l
           // DONT FORGET TO CLOSE
           // this will cause all the numbers to be shown in a straight line
           // as this: 38472095823405720384
           // experiment with this class and have fun learning new things!
           catch (IOException e) {
                // this will catch the exception
                // and print it out onto console
                // for you to read easier 


The next common method that is learned when starting Java is the method called printf(). This method is used to show information in specific ways to the user. The most common ways of printf() are:

* Major programs that require using indentation

  • Programs that have to be aligned in text
  • Word processors

* Big applications that display information in a specific format.

Here, we will write a program that will take a String (words), an int, and a float value.

Before we begin, there are some things any person using printf() needs to know:

String Formatting Integer Formatting Float Formatting
1 space of text %s 1 space for int: %1d or %1i 1 space for float: %1f
10 spaces: %10s 10 spaces for int: %10d or %10i 10 spaces with 2 decimal places after decimal: %10.2f
100 spaces: %100s 100 spaces for int: %100d or %100i 100 spaces with 15 decimal places: %100.15f

NOTE: The table above is only with the most basic usage of ''printf()'' There is not enough time to explain the full uses of the function, trial and run is the best way

Now that we have that all cleared, let's write our program!

public class WriteUsingPrintF {
   public static void main(String[] args) {
      try {
          String formattedString = "formatting";
          int integer = 100;
          float floatNumber = 100f;
          File f = new File("formatFile.txt");
          PrintWriter printWriter = new PrintWriter(f);
          // writing the formatted string to show 1 space of text
          printWriter.printf("%s\n", formattedString);
          // writing the formatted string to show 10 spaces of text
          printWriter.printf("%10s\n", formattedString);
          // writing the formatted string to show 100 spaces of text
          printWriter.printf("%100s\n", formattedString);
          // writing the formatted int to show 1 space of number
          printWriter.printf("%i\n", integer);
          // writing the formatted int to show 10 spaces of number
          printWriter.printf("%10i\n", integer);
          // writing the formatted int to show 100 spaces of number
          printWriter.printf("%100i\n", integer);
          // writing the formatted float to show 1 space of number
          printWriter.printf("%1f\n", floatNumber);
           // writing the formatted float to show 10 spaces of number with 2 decimal places 
          printWriter.printf("%10.2f\n", floatNumber);
           // writing the formatted float to show 100 spaces of number with 15 decimal places 
          printWriter.printf("%100.15f\n", floatNumber);
           // making sure to close the file!
      } catch (IOException e) {

What Next?

Take your newly learned skills, and progress them. Don't be afraid to crash your program by messing up a few lines! It won't cause your computer to explode! ^_^

Write programs that write files. Write a program that writes a story by adding on a certain character so many times. The possibilities are endless!

I would LOVE to hear from you!! Tell me all about your program(s), the way you made them, and how you came about making them!

Email me at:

on the occasion of the current invasion of Russia in Ukraine

Russian Stop this War
java/io/printwriter.txt · Last modified: 2024/02/16 01:04 (external edit)

Impressum Datenschutz