13.07.2015 Views

I/O Fundamentals

I/O Fundamentals

I/O Fundamentals

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Java Input and OutputYou've already seen how the PrintWriter and PrintStream classes work. Whatyou may not have realized is that the way their println methods work is to writethe appropriate new-line character based on the current platform. They get thatinformation from the system property line.separator.To use this information directly, you could write code as follows:String newLine = System.getProperty("line.separator");writer.write("This is a test" + newLine +"followed by another line" + newLine +"and another");The line.separator property is given a value by the VM. Because the VMknows the platform on which it is running, it can choose the proper new-linecharacter sequence.Slash and Path Separation CharactersOf similar concern are two path-related issues:• which character is used to separate directories when specifying a file's path• which character is used to separate files/directories in "path sequences"The first issue may not seem too difficult, as you've already see that when Javacreates a File object it "does the right thing" with regard to slashes.However, the caution here has more to do with what information you show yourusers, or write to a generated batch/script file.When presenting a file name to the user, if you create a File object and displayone of its path names, it will be presented appropriately. If instead you needsimply to put together a file name and show it to the user or write it to a file, youmay be in for some confusion.Suppose your program is running on Windows and contains the following code:System.out.println("I cannot file images/smurf.gif!");While many Windows users will interpret the message properly, some who thinkmore literally may see the message and think "what's the 'smurf.gif' option to the'images' program???"If the situation is reversed, running on UNIX with the following code:System.out.println("I cannot file images\\smurf.gif!");© 1996-2003 jGuru.com. All Rights Reserved. Java Input and Output -53

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!