> That's because it is not a statement. It is a declaration.
Ah. Yes, I see the problem now.
> If you think about it, it does not make much sense for the else clause of
> an if statement to consist of a declaration. Its scope would be just the
> else clause, so the identifier being declared cannot be referred to.
I was under the impression, for some reason, that the line would create a
file and associated reference object for use later on in the program if a
file by the specified name didn't already exist.
John McGrath - 15 Apr 2005 00:33 GMT
> > If you think about it, it does not make much sense for the else clause
> > of an if statement to consist of a declaration. Its scope would be
[quoted text clipped - 4 lines]
> a file and associated reference object for use later on in the program
> if a file by the specified name didn't already exist.
I do not understand what this has to do with the text that you quoted.
I was talking about the Java language and the scope that the declaration
would have had were it legal there. You seem to be talking about how a
FileWriter works.

Signature
Regards,
John McGrath
Paul Fedorenko - 15 Apr 2005 03:10 GMT
> I do not understand what this has to do with the text that you quoted.
> I was talking about the Java language and the scope that the declaration
> would have had were it legal there. You seem to be talking about how a
> FileWriter works.
Simple matter of misunderstanding on my part of how file I/O in Java works.
I think I'm well on the way to getting it sorted out. I'd better be, at any
rate, since the thing's due tomorrow. <grin>
On a related note... How would I do something like:
if (specified file doesn't exist)
{
do stuff;
}
else
{
System.out.println("file " + args[1] + " already exists");
return;
}
Actually, that wouldn't work either. The program needs to check for the
existance of a file with the specified name. If the file doesn't exist, a
new file is supposed to be created. If the file DOES exist, the contents of
the above else block get executed.
To create a new output file, I'm using: File file = new File(args[1]);
which creats the second file specified at the commandline (the first is
assumed to exist, since that's the one you're copying).
Unfortunately, for the line: if (file.exists()) to work, the previous
statement needs to be put into the code before the test, and if I do that,
the "file exists" test fails and the program ends because a new file can't
be created because it's already there.
Part of the problem that I'm running into is that for the assignment, I'm
supposed to rewrite a program that uses byte streams so that it uses
FileReader and FileWriter instead. The problem is that the book
("Introduction to Java Programming" by Y. Daniel Liang), doesn't actually
mention *anywhere* in the chapter in I/O how FileReader and FileWriter are
actually supposed to be used.
The instructor gave us an example of how those two methods are used, and
I've used it pretty much word-for-word in my program, except that I used
args[] instead of specifying file names.
John McGrath - 15 Apr 2005 00:33 GMT
> To create a new output file, I'm using: File file = new File(args[1]);
> which creats the second file specified at the commandline (the first is
[quoted text clipped - 4 lines]
> that, the "file exists" test fails and the program ends because a new
> file can't be created because it's already there.
Creating a java.io.File object does not create a disk (or whatever) file.
A File object just encapsulates the name and path of a file.
> Part of the problem that I'm running into is that for the assignment,
> I'm supposed to rewrite a program that uses byte streams so that it uses
> FileReader and FileWriter instead. The problem is that the book
> ("Introduction to Java Programming" by Y. Daniel Liang), doesn't
> actually mention anywhere in the chapter in I/O how FileReader and
> FileWriter are actually supposed to be used.
Check the JavaDocs.
> The instructor gave us an example of how those two methods are used, and
> I've used it pretty much word-for-word in my program, except that I used
> args[] instead of specifying file names.
A stylistic comment: I think that writing your file copying code using
args[1] as the file name argument is a bad idea. It would be far more
readable if you separated your command line processing from the code that
copies the file. One simple approach is to start your main program out
with something like this:
public static void main( String[] args ) {
if ( args.length != 2 ) {
System.err.println( "....." );
System.exit( 1 );
}
String inputFile = args[0];
String outputFile = args[1];
// file copying.
}
Even better, move the file copying code into a separate method.

Signature
Regards,
John McGrath
Paul Fedorenko - 15 Apr 2005 06:42 GMT
Ok. I got it. Thanks for the suggestions. I ended up deleting the code
that I had and starting from scratch with a completely different approach.
It seems to work properly, so I'm handing it in as is.