How to Write to a file with JAVA

The task of writing to a file with Java is greatly simplified with Input/Output streams. These are a group of classes used for basic I/O and include classes for serialization that allows a program to read and write entire objects to a stream. Many methods of writing to a file can be employed and they are included in the java.io package. Here's a simple example of writing to a file with Java.

Instructions

    • 1

      Import all of the needed classes. We import each class individually for the purposes of illustration but we could import the entire package with the statement "import java.io.*."

    • 2

      Declare a FileInputStream object for the input and output files and instantiate them with the desired input and output file names (input.txt and output.txt, respectively).

    • 3

      Use the write method of the FileOutputStream class to write to the output file. Other methods are available for writing to a file, but write is used here as the simplest possible example.

    • 4

      Notice how the program stays in a loop that reads a byte from the input stream and writes the byte to the output stream until the end of the input file is reached.

    • 5

      Look at the complete code for this example:

      import java.io.FileInputStream;
      import java.io.FileOutputStream;
      import java.io.IOException;

      public class WriteBytes {
      public static void main(String[] args) throws IOException {
      FileInputStream in = null;
      FileOutputStream out = null;
      try {
      int i;
      in = new FileInputStream("input.txt");
      out = new FileOutputStream("output.txt");

      while ((i = in.read()) != -1){
      out.write(i);
      }
      }
      finally {
      if (in != null) {
      in.close();
      }
      if (out != null) {
      out.close();
      }
      }
      }
      }

Related Searches:

Resources

Comments

You May Also Like

  • How to Write a Method in Java

    Java is the most popular programming language, primarily because of its portability. The same Java program can be used on virtually any...

  • How To Write Java Programs

    Java is an object-oriented programming language originally developed and released by Sun Microsystems in the mid-1990s. Java was developed from the start...

  • How to Write to a Text File From JSP

    Writing to a text file from JSP is just as easy to do as it is in Java. With the proper knowledge...

  • How to Create a Java File

    Creating and writing data to a file in Java can be a slightly counter-intuitive task since the creation of the built-in Java...

  • How to Write a Batch File to Copy a Folder

    Batch files are small executable files that run within the Microsoft operating systems. These files, whose names end with a .bat extension,...

  • How to Write Files to CD

    Writing a file to CD does not require much job as many people thought it to be. There are some points though...

  • How to Write a 3D Java Game

    Java 3-D games are based on a concept called a "scene graph," which simplifies game development. After conceiving the idea for a...

Related Ads

Featured