• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Not writing to a text file.

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
}
}
}
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sudha,
On windows, crtl + z will indicate end of file.
cheerio
rowan
 
Peter Chase
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic