Java Reference
In-Depth Information
Table 3.2: How Operating Systems End Lines
Operating System
ASCII Codes
Java
UNIX
#10
"\n"
Windows
#13 #10
"\r\n"
Mac OSX
#10
"\n"
Mac Classic
#13
"\r"
To properly download a text file, the program must make sure that the line breaks are
compatible with the current operating system. Since Java can be run on a variety of operating
systems, this is especially important.
To use this recipe to download the index page of the HTTP recipes site, you would use
the following command:
DownloadText http://www.httprecipes.com/ ./contents.txt
The above command simply shows the abstract format to call this recipe, with the ap-
propriate parameters. For exact information on how to run this recipe refer to Appendix B, C,
or D, depending on the operating system you are using. The above arguments will download
the HTML text to a file named contents.txt .
Listing 3.7 shows how this is done.
Listing 3.7: Download a Text File (DownloadText.java)
package com.heatonresearch.httprecipes.ch3.recipe5;
import java.io.*;
import java.net.*;
public class DownloadText
{
/**
* Download the specified text page.
*
* @param page The URL to download from.
* @param filename The local file to save to.
*/
public void download(String page, String filename)
{
try
{
URL u = new URL(page);
Search WWH ::




Custom Search