Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Not writing to a text file.  RSS feed

 
Sudha Kris
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
}
}
}
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you run on Windows. If so, CTRL-C is not a good way to indicate end of input. CTRL-C abruptly aborts the Java process, giving it no chance to flush buffers. I am not sure what, if any, keystroke would be better on Windows; on UNIX, CTRL-D is "end of file".
 
Sudha Kris
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for the reply. So now what's the solution to that, say instead of checking for EOF, I can change the condition to
say "type END to exit" but that doesn't work either.
Thanks
Sudha
 
Rowan Brownlee
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sudha,
On windows, crtl + z will indicate end of file.
cheerio
rowan
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I just noticed that you close your FileWriter before you close your PrintWriter. That seems wrong. You want to close your PrintWriter, flushing its buffers, while the FileWriter is still open, making it possible for information flushed from the PrintWriter's buffers to be written to the FileWriter. Close the FileWriter last.
 
Sudha Kris
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Rowan thanks, for the reply. Now it works. Peter, it doesn't matter if I close FileWriter first or PrintWriter first, 'cos I have closed the buffer before both. The data is in the buffer.
Thanks
Sudha
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad it works, but I do maintain that it may matter which way around you have your close() statements.
The BufferedInputStream is closed before the FileWriter and the PrintWriter, but closing this is not relevant here, as you have already established that its buffer is empty, otherwise you would have been able to continue reading data.
But when writing to the PrintWriter, the written data goes into its buffer. This buffer may not have been flushed when you close the FileWriter. Thus there appears to be a risk of data loss.
In practice, you clearly find that there is no data loss. This may be pure luck, it may be platform-dependent (i.e. it always works on Windows, but may not on another platform) or it may be that the way that the PrintWriter/FileWriter classes are written guarantees that it always works (look at the source).
Or am I still missing something?
 
Sudha Kris
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Peter thanks for clearing my doubt. I agree with you it may work fine in Windows but may be not in other platforms.
Sudha
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!