Curl Global Community
Files over HTTP1:Files over HTTP - Printable Version

+- Curl Global Community (https://communities.curl.com)
+-- Forum: Tutorials (https://communities.curl.com/forumdisplay.php?fid=3)
+--- Forum: Public Training (https://communities.curl.com/forumdisplay.php?fid=4)
+---- Forum: Curl Clues (https://communities.curl.com/forumdisplay.php?fid=5)
+---- Thread: Files over HTTP1:Files over HTTP (/showthread.php?tid=16)



Files over HTTP1:Files over HTTP - kino - 06-15-2011

File Access
It is common for programs to read text from and write text to local or remote data files. An applet may need to read data from a Web page, or write data to the local machine for use the next time the program is run.


Reading from and writing to resources are generally accomplished the same way. A resource is identified, a stream is opened to it, the data is transferred, and the resource is closed. The resource is typically identified using a Url object. This Curl Cue will demonstrate how to read file resources. The examples will use text files obtained via HTTP, but it is worth noting that file access is the same whether the file is local or obtained over HTTP.


An input stream object provides the methods necessary to transfer the data. There are general classes and methods that provide the essential means for reading and writing data, but there are also high-level, convenience procedures that simplify many tasks.


The easiest way to read data from a resource is to treat the resource as a text file. Many resources, such as configuration files and Web pages, are commonly handled this way. In all textual cases, you will have to:

1.Identify the Url

2.Create a TextInputStream to read the data

3.Read and store the data

4.Close the TextInputStream


Reading File Contents
So, how do you read the contents of a file? For our example, we will access the business.txt file that has the following content:


Local Businesses

Bakery

Convenience Store

Coffee Shop

Print Shop

Chiropractor

Fitness Center

Acupuncture

Photo Shop

Optician

Shoe Store


The easiest way to read the entire contents of a file is to use the read-from procedure. This procedure is actually a utility procedure that combines several operations:

1.It automatically opens the given url as a TextInputStream

2.It reads all the characters out of it into a StringBuf

3.It returns the StringBuf.

Code:
{curl 6.0, 7.0 applet}

{pre {read-from {url "business.txt"}}}

The pre text format displays the paragraph contents in a monospace font, preserving whitespace and newlines.



Once the file is read, the program can manipulate the StringBuf, or in the case above, we just displayed the content using pre. You may need to convert the content to a String for further processing, for example: {{read-from {url "business.txt"}}.to-String}.


Reading Line by Line
One of the most common ways to read text from a file is line by line. Most text files will have multiple lines to read, so this approach makes sense, especially if a line needs to be processed by the applet before the next line is read.


The next example demonstrates a simple way to read a text file line by line.
Code:
{curl 6.0, 7.0 applet}

{value
   || Create a StringBuf to hold the data
   let lines:StringArray = {StringArray}
  
   || Identify the url
   let location:Url = {url "business.txt"}

   {with-open-streams in = {read-open location} do
       {until in.end-of-stream? do
           {lines.append {{in.read-one-line}.to-String}}
       }
   }

   || Display the stored information
   let display:VBox =  
       {VBox
           background = "beige",
           border-style = "sunken",
           border-width = 2pt,
           border-color = "navy",
           margin = 4pt
       }
   {for line key k in lines do
       {if k==0 then
           {display.add {bold {underline {value line}}}}
        else
           {display.add line}
       }
   }
  
   display
}
We use with-open-streams to read each line of a given file and store it in a StringArray. Notice that we detect the end of the file using the end-of-stream? accessor.

Code:
{with-open-streams in = {read-open location} do
       {until in.end-of-stream? do
           {lines.append {{in.read-one-line}.to-String{font}
       }
  }
For the display VBox content, we looped through the lines StringArray. The loop is organized so that the first line will be displayed underlined and bolded.

Code:
{for line key k in lines do
       {if k==0 then
           {display.add {bold {underline{value line{font}{font}
        else
           {display.add line}
       }
  }
In this Curl Cue, we identified two main ways to read file content. The Curl language has many stream classes that can be used based on your application needs. These include:


•Text and binary streams

•Buffered and unbuffered streams

•Seekable and nonseekable streams



For more information regarding reading file content, please refer to the following section in the Curl Documentation:Curl Developer's Guide > File Systems and Networking > Accessing Files and Other Resources