Home‎ > ‎Applied Core Java‎ > ‎Basic I/O‎ > ‎

Read from Keyboard and Write to a File

Problem : How you can read from a Keyboard and write to a text file, just like DOS copycon command?

Solution :
Keyboard is represented by an object System.in. System.in.read() method returns bytes for charters those are entered from keyboard.  We need a filter class  java.io.InputStreamReader  to convert bytes into characters.  But we want to read data line by line so charters are converted into lines with help of java.io.BufferedReader class.

Lines then will be written to a file with help of java.io.PrintWriter class. PrintWriter in turn connected with FileWrite to write charcetrs on a file.

Is that clear ?? Lets start a sample program.

Program :

Step 1 - Coding

Create a text file c:/sunilos/Copycon.java and copy below contents.

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;

/**
 * A program to read data from keyboard and write to file until 'quit' sting is entered.
 *
 */
public class Copycon {

    public static void main(String[] args) throws Exception {
        String target ="c:/sunilos/sunilos-keyboard.txt";
        FileWriter writer = new FileWriter(target);
        PrintWriter printWriter = new PrintWriter(writer);
       
        InputStreamReader inputStreamReader = new InputStreamReader(System.in);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
       
        String line = bufferedReader.readLine();
        while(!line.equals("quit")){
            printWriter.print(line);
            line = bufferedReader.readLine();
        }
        printWriter.close();
        inputStreamReader.close();

    }

}

Step 2 - Deployment

  1. Create a folder 'c:\sunilos'. 
  2. Create or copy Copycon.java into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile Copycon.java with help of 
    • javac Copycon.java command. Command will create Copycon.class file in the same folder.
  5. Congratulations!! your Java program is ready to serve.

Step 3 - Testing

  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your java program from command prompt with help of 
    • java Copycon command.
  3. Now start writing multiple lines on command prompt. To exit write 'quit' and press enter key.
  4. Now open and check your entered data.
    • c:/sunilos/sunilos-keyboard.txt You got it ? Congratulations!!

Output


C:\sunilos>
>Hi
>How R U ?
>I Pretty Good, I am in hurry
>Bye
>quit

FAQ

What is InputStreamReader?

  • An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters.

Most Interesting methods in InputStreamReader?

  • close() - Close the stream.
  • getEncoding() -  Return the name of the character encoding being used by this stream.
  • read() - Read a single character.
  • ready() - Tell whether this stream is ready to be read.

<<Previous | Next>>
ċ
Copycon.java
(1k)
Mahima Hardia,
Aug 19, 2011, 11:03 PM
Comments