Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

How to Use I/o Streams in Java

Learn how to use I/o streams in java with clear steps, practical context, and useful troubleshooting guidance.

Table of Contents

This updated guide examines How to Use I/o Streams in Java and organizes the essential facts, background, and practical takeaways in clear American English.

Method 1

Using Byte and Character Streams

  1. How to Use I/o Streams in Java — contextual image 1 Create a text file. Give it an arbitrary name and write something in it, such asI love wikiHow!. In this article, we will name itfile.txt.
  2. How to Use I/o Streams in Java — contextual image 2 Create a main method. You can addthrows IOExceptiondeclaration to the method signature to avoid adding the catch block to the try-with-resources block.
    publicstaticvoidmain(String[]args){}
  3. How to Use I/o Streams in Java — contextual image 3 Create a try-with-resources block. Try-with-resources block will automatically close streams for us.
    publicstaticvoidmain(String[]args){try(){}}
  4. How to Use I/o Streams in Java — contextual image 4 CreateFileInputStreamorFileReaderin the block statement. In the constructor of the created input stream, specify the name of the text file previously created. In this article, we called itfile.txt.
    publicstaticvoidmain(String[]args){try(FileInputStreamin=newFileInputStream("file.txt")){}}
    • The former is a byte stream, the latter is a character stream.
  5. How to Use I/o Streams in Java — contextual image 5 CreateFileOutputStreamorFileWriterin the block statement. In the constructor of the created output stream, specify another arbitrary text file name. In this article, we will name itfile-copy.txt.
    publicstaticvoidmain(String[]args){try(FileInputStreamin=newFileInputStream("file.txt");FileOutputStreamout=newFileOutputStream("file-copy.txt")){}}
    • If you createdFileInputStream, you need to createFileOutputStream. The same goes for theFileReaderandFileWriter.
  6. How to Use I/o Streams in Java — contextual image 6 Createintvariable. This variable will be used for temporary storage of read byte or character.
    publicstaticvoidmain(String[]args){try(FileInputStreamin=newFileInputStream("file.txt");FileOutputStreamout=newFileOutputStream("file-copy.txt")){intdata;}}
  7. How to Use I/o Streams in Java — contextual image 7 Read data from input stream. Create a while loop in which the data is read into the previously createdintvariable until it reads the number-1. In other words, it reads data from the file until it reaches the end of the file.
    publicstaticvoidmain(String[]args){try(FileInputStreamin=newFileInputStream("file.txt");FileOutputStreamout=newFileOutputStream("file-copy.txt")){intdata;while((data=in.read())!=-1){}}}
  8. How to Use I/o Streams in Java — contextual image 8 Write data to the output stream. In the body of the while loop, write the read data to the output stream.
    publicstaticvoidmain(String[]args){try(FileInputStreamin=newFileInputStream("file.txt");FileOutputStreamout=newFileOutputStream("file-copy.txt")){intdata;while((data=in.read())!=-1){out.write(data);}}}
  9. How to Use I/o Streams in Java — contextual image 9 Catch and handle IOException. Add a catch block to the try-with-resources block, catchIOExceptionand, in case of an error, print the stack trace to the console.
    publicstaticvoidmain(String[]args){try(FileInputStreamin=newFileInputStream("file.txt");FileOutputStreamout=newFileOutputStream("file-copy.txt")){intdata;while((data=in.read())!=-1){out.write(data);}}catch(IOExceptione){e.printStackTrace();}}
  10. How to Use I/o Streams in Java — contextual image 10 Run the program. Contents of thefile.txtare copied to thefile-copy.txtwhich, if it doesn't exist yet, is created.

Method 2

Using Buffered Streams

Buffered streams optimize input and output data. Buffer streams are more efficient than byte and character streams as they read and write from a buffer that is handled by Java platform rather than the underlying operating system. Most common usage of buffered streams is reading a textual file, line by line.

  1. How to Use I/o Streams in Java — contextual image 11 Create a text file. Give it an arbitrary name and write something in it, such asI love wikiHow!. In this article, we will name itfile.txt.
  2. How to Use I/o Streams in Java — contextual image 12 Create a main method. You can addthrows IOExceptiondeclaration to the method signature to avoid adding the catch block to the try-with-resources block.
    publicstaticvoidmain(String[]args){}
  3. How to Use I/o Streams in Java — contextual image 13 Create a try-with-resources block. Try-with-resources block will automatically close streams for us.
    publicstaticvoidmain(String[]args){try(){}}
  4. How to Use I/o Streams in Java — contextual image 14 CreateBufferedReaderin the block statement. In the constructor of the created input stream, create aFileReaderand specify the name of the text file previously created inside its constructor. In this article, we called itfile.txt.
    publicstaticvoidmain(String[]args){try(BufferedReaderin=newBufferedReader(newFileReader("file.txt"))){}}
  5. How to Use I/o Streams in Java — contextual image 15 CreateBufferedWriterin the block statement. In the constructor of the created output stream, create aFileWriterand specify another arbitrary text file name in its constructor. In this article, we will name itfile-copy.txt.
    publicstaticvoidmain(String[]args){try(BufferedReaderin=newBufferedReader(newFileReader("file.txt"));BufferedWriterout=newBufferedWriter(newFileWriter("file-copy.txt"))){}}
  6. How to Use I/o Streams in Java — contextual image 16 CreateStringvariable. This variable will be used for temporary storage of read lines.
    publicstaticvoidmain(String[]args){try(BufferedReaderin=newBufferedReader(newFileReader("file.txt"));BufferedWriterout=newBufferedWriter(newFileWriter("file-copy.txt"))){Stringline;}}
  7. How to Use I/o Streams in Java — contextual image 17 Read lines from input stream. Create a while loop in which the lines are read into the previously createdStringvariable until it readsnull. In other words, it reads data from the file until it reaches the end of the file.
    publicstaticvoidmain(String[]args){try(BufferedReaderin=newBufferedReader(newFileReader("file.txt"));BufferedWriterout=newBufferedWriter(newFileWriter("file-copy.txt"))){Stringline;while((line=in.readLine())!=null){}}}
  8. How to Use I/o Streams in Java — contextual image 18 Write lines to the output stream with line separator. In the body of the while loop, write the read lines to the output stream and append the line separator at the end of each line.BufferedReaderreads lines, but omits the line separator.
    publicstaticvoidmain(String[]args){try(BufferedReaderin=newBufferedReader(newFileReader("file.txt"));BufferedWriterout=newBufferedWriter(newFileWriter("file-copy.txt"))){Stringline;while((line=in.readLine())!=null){out.write(line+System.lineSeparator());}}}
  9. How to Use I/o Streams in Java — contextual image 19 Catch and handle IOException. Add a catch block to the try-with-resources block, catchIOExceptionand, in case of an error, print the stack trace to the console.
    publicstaticvoidmain(String[]args){try(BufferedReaderin=newBufferedReader(newFileReader("file.txt"));BufferedWriterout=newBufferedWriter(newFileWriter("file-copy.txt"))){Stringline;while((line=in.readLine())!=null){out.write(line+System.lineSeparator());}}catch(IOExceptione){e.printStackTrace();}}
  10. How to Use I/o Streams in Java — contextual image 20 Run the program. Contents of thefile.txtare copied to thefile-copy.txtwhich, if it doesn't exist yet, is created.

Method 3

Using Data Streams

Data streams handle binary input and output of primitive data types and Strings. Data streams make it very easy to read and writeboolean,char,byte,short,int,long,float,doubleandStringvalues. Most common usage of data streams is in network applications (e.g. instant messengers).

  1. How to Use I/o Streams in Java — contextual image 21 Create a main method. You can addthrows IOExceptiondeclaration to the method signature to avoid adding the catch block to the try-with-resources block.
    publicstaticvoidmain(String[]args){}
  2. How to Use I/o Streams in Java — contextual image 22 Create a try-with-resources block. Try-with-resources block will automatically close streams for us.
    publicstaticvoidmain(String[]args){try(){}}
  3. How to Use I/o Streams in Java — contextual image 23 CreateDataOutputStreamin the block statement. In the constructor of the created input stream, createFileOutputStreamand specify an arbitrary file name inside its constructor. In this article, we will name itfile.txt.
    publicstaticvoidmain(String[]args){try(DataOutputStreamout=newDataOutputStream(newFileOutputStream("file.txt"))){}}
  4. How to Use I/o Streams in Java — contextual image 24 CreateDataInputStreamin the block statement. In the constructor of the created input stream, create aFileInputStreamand specify file name from the previous step inside its constructor. In this article, we named itfile.txt.
    publicstaticvoidmain(String[]args){try(DataOutputStreamout=newDataOutputStream(newFileOutputStream("file.txt"));DataInputStreamin=newDataInputStream(newFileInputStream("file.txt"))){}}
  5. How to Use I/o Streams in Java — contextual image 25 Write some data to the output stream. In the body of the try-with-resources block, write String,boolean,intand other primitive data to the output stream.
    publicstaticvoidmain(String[]args){try(DataOutputStreamout=newDataOutputStream(newFileOutputStream("file.txt"));DataInputStreamin=newDataInputStream(newFileInputStream("file.txt"))){out.writeUTF("wikiHow is a great place to learn");out.writeBoolean(true);out.writeInt(10430);out.writeDouble(20.9d);out.writeFloat(23.10f);}}
  6. How to Use I/o Streams in Java — contextual image 26 Read data from the input stream and print it. Read the data in the same order as it has been written and print it to the console.
    publicstaticvoidmain(String[]args){try(DataOutputStreamout=newDataOutputStream(newFileOutputStream("file.txt"));DataInputStreamin=newDataInputStream(newFileInputStream("file.txt"))){out.writeUTF("wikiHow is a great place to learn");out.writeBoolean(true);out.writeInt(10430);out.writeDouble(20.9d);out.writeFloat(23.10f);System.out.println(in.readUTF());System.out.println(in.readBoolean());System.out.println(in.readInt());System.out.println(in.readDouble());System.out.println(in.readFloat());}}
  7. How to Use I/o Streams in Java — contextual image 27 Catch and handle IOException. Add a catch block to the try-with-resources block, catchIOExceptionand, in case of an error, print the stack trace to the console.
    publicstaticvoidmain(String[]args){try(DataOutputStreamout=newDataOutputStream(newFileOutputStream("file.txt"));DataInputStreamin=newDataInputStream(newFileInputStream("file.txt"))){out.writeUTF("wikiHow is a great place to learn");out.writeBoolean(true);out.writeInt(10430);out.writeDouble(20.9d);out.writeFloat(23.10f);System.out.println(in.readUTF());System.out.println(in.readBoolean());System.out.println(in.readInt());System.out.println(in.readDouble());System.out.println(in.readFloat());}catch(IOExceptione){e.printStackTrace();}}
  8. How to Use I/o Streams in Java — contextual image 28 Run the program. Data is written to thefile.txtand then read and printed to the console.
    wikiHowisagreatplacetolearntrue1043020.923.1

Method 4

Using Object Streams

Object streams handle binary input and output of Java objects. Like data streams, object streams make it very easy to read and write primitive data types and Strings, but also any object that implementsSerializableinterface. Most common usage of object streams is in network applications where both server and client are written in Java.

    1. How to Use I/o Streams in Java — contextual image 29 Create a serializable class. Create a class that implementsSerializableinterface. Name the class however you'd like, but in this article we will name itWikiHowian.
      publicclassWikiHowianimplementsSerializable{}
    2. How to Use I/o Streams in Java — contextual image 30 Create a few private (serializable) variables. SinceWikiHowianclass represents a person, we will create two String variables for first and last name and anintvariable for age.
      publicclassWikiHowianimplementsSerializable{privateStringfirstName,lastName;privateintage;}

    FAQ

    What is How to Use I/o Streams in Java about?

    It provides a structured overview of file, explains the main context, and highlights practical takeaways for readers.

    Why does this topic matter?

    Understanding the main concepts helps readers evaluate the issue, avoid common mistakes, and make better-informed decisions.

    How should readers use this information?

    Use the guidance as a practical starting point, confirm details that may have changed, and follow current product, safety, or security recommendations.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.