Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
- /*
- * Create a program which takes input from the console and writes to a file using BufferedWriter.
- */
- import java.io.*;
- class BufferedWriterDemo
- {
- public static void main(String args[])
- {
- try
- {
- DataInputStream in = new DataInputStream(System.in);
- FileWriter fw = new FileWriter("C:/IO_demo.txt");
- BufferedWriter bw = new BufferedWriter(fw);
- System.out.println("How many lines?");
- int numberOfLines = Integer.parseInt(in.readLine());
- String line = "";
- for(int i = 0; i < numberOfLines; i++)
- {
- line = in.readLine();
- bw.write(line + "\n"); // throws IOException
- }
- bw.close();
- }
- catch(Exception e)
- {
- System.out.println(" Exception : " + e);
- }
- }
- }

Post new comment