Hi,
See the following code. This is to read a file from input(System.in) & write it to a text file.but it's not working. Can anyone tell me why?
Thanks
Sudha
/* This program uses the CheckFile class to check
a file typed in by the user as a command-line argument
*/
import java.io.*;
class ReadWrite
{
public static void main(
String[] args)
{
String line="S";
try
{
// Create file
File file = new File(args[0]);
System.out.println("Enter anything to read");
System.out.println("Type ctrl-c to exit.");
//Create a FileWriter for the file
FileWriter fWriter = new FileWriter(file);
PrintWriter pWriter = new PrintWriter(fWriter);
InputStreamReader inStream = new InputStreamReader(System.in);
BufferedReader buf = new BufferedReader(inStream);
while((line= buf.readLine())!=null)
{
System.out.println(line);
pWriter.println(line);
}
buf.close();
fWriter.close();
pWriter.close();
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Must enter a file name as a command-line argument");
}
catch(IOException ie)
{
System.out.println("Error in reading file");
}
}
}