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 FileWriter.
- */
- import java.io.*;
- class FileWriterDemo
- {
- public static void main(String args[])
- {
- try
- {
- DataInputStream in = new DataInputStream(System.in);
- FileWriter fw = new FileWriter("C:/IO_demo.txt");
- System.out.println("How many lines?");
- int numberOfLines = Integer.parseInt(in.readLine());
- String line = "";
- for(int i = 0; i < numberOfLines; i++)
- {
- line = in.readLine();
- fw.write(line + "\n"); // throws IOException
- }
- fw.close();
- }
- catch(Exception e)
- {
- System.out.println("Exception : " + e );
- }
- }
- }

Post new comment