Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re:copying file



On Monday, April 26, 2004, Daniel Child wrote:

This approach (found where you suggested in the Java tutorial) is
obviously more streamlined than what I was doing. But TextEdit still
failed to open the new rtf file!!! The file and its icon appear in my
Build folder next to the original, but the open fails.

Testing for success by trying to open the file with
TextEdit does not provide much feedback. You should also
compare the files in other ways, looking for differences
(e.g. "ls -l FILE", "cksum FILE", "hexdump -C File" or
"diff F1 F2" in a Terminal window). You can instrument
your code, doing things like adding a byte counter to
report how many bytes were written, tracking the min and
max byte value copied, or even echoing each char read in
to System.out. Basically it is not enough to know that
the file was copied incorrectly, you need to work out
what is incorrect, and from that deduce why your program
is producing the observed result.

int c;
while ((c = fis.read()) != -1) {
fos.write(c);
}

Although the code above is technically correct, it uses a
side effect (the assignment to c buried inside the while
conditional expression), which can obscure the meaning of
the code, especially to a beginner. The code above could
be written as:

while (true) {
int c = fos.read();
if (c == -1) break;
fos.write(c);
}

making its function a little more obvious. In programming,
clarity is always preferable to brevity (at least after one
has spent sufficient time debugging other people's code).
The compiler should emit equivalent byte code for each, so
runtime performance is not a consideration.


Doug Zwick, Software Developer /---------------------------------------------/
http://www.elluminate.com/ / "Thank God we don't get all the Government
email@hidden / we pay for." -- Will Rogers
============================/
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Do not post admin requests to the list. They will be ignored.




Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.