Database
  Home arrow Database arrow Page 5 - Reading and Writing to Files on the Intran...
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE

Reading and Writing to Files on the Intranet
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2006-10-10

    Table of Contents:
  • Reading and Writing to Files on the Intranet
  • Opening the Stream Object
  • Can you read text in another language?
  • Writing to a file with an HTM extension
  • Reading and writing binary files

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Reading and Writing to Files on the Intranet - Reading and writing binary files
    (Page 5 of 5 )

    Reading and writing binary data is almost identical to reading and writing text except that the read() and write() methods have to be used. Since binary is one whole file, there are no line separators as in text. The Read() and write() methods accept byte values and return byte values.

    Reading a BMP file and persisting it to another location

    This example uses the stream object to read a bitmap file on one of the folders and saves it to another folder on the intranet using the SaveToFile() method. The red.bmp file represents a 16 x 16 square filled with red color and takes up 821 bytes.

    Private Sub Command1_Click()
    Dim strm As ADODB.stream
    Set strm = New ADODB.stream
    'Stream type is binary
    strm.Type = adTypeBinary
    'open the file red.bmp
    strm.Open "Url=http://localhost/DevHome/DevArticles/red.bmp"
    'save the file to root directory of ASPFree
    strm.SaveToFile "C:InetpubwwwrootDevHomeASPFreeTest.bmp", _
    adSaveCreateOverWrite
    strm.Close
    End Sub
    

    When the Test.bmp is browsed you will see the saved bmp file as shown.

    Reading a binary file and placing it in a buffer

    The same red.bmp file can be read to a buffer as follows. The bytes read into the buffer are in the bufread variable. The upper bound of this shows the size of the red.bmp file read, which is 821 bytes. The read also adds an extra byte at the end position.

    Private Sub Command2_Click()
    Dim bufread() As Byte
    Dim strm As ADODB.stream
    Set strm = New ADODB.stream
    strm.Type = adTypeBinary
    strm.Open "URL=http://hodentek/DevHome/DevArticles/red.bmp"
    bufread = strm.Read(ADODB.StreamReadEnum.adReadAll)
    MsgBox (UBound(bufread))
    Dim mycount As Long
    For mycount = 0 To UBound(bufread)
    Debug.Print mycount & "," & (bufread(mycount))
    Next mycount
    strm.Close
    End Sub

    Writing a binary file

    The data that gets into the buffer will be a byte array and the Stream's write method will write to this array as shown in the listing.

    Option Compare Database
    Private Sub Command0_Click()
    Dim mybuf(3) As Byte
    Dim rec As ADODB.Record
    Dim strm As ADODB.stream
    Set rec = New ADODB.Record
    Set strm = New ADODB.stream
    rec.Open "jay.dat", "URL=http://hodentek/DevHome/DevShed/", _
    adModeReadWrite, adCreateNonCollection + adCreateOverwrite
    strm.Type = adTypeBinary
    strm.Open rec, adModeReadWrite, adOpenStreamFromRecord
    mybuf(0) = 100
    mybuf(1) = 25
    mybuf(2) = 210
    strm.Write (mybuf)
    'jay.dat file with 4 Bytes will be created
    'with the 4th byte, the end of file
    strm.Close
    rec.Close
    End Sub

    Summary

    The tutorial describes with examples writing and reading from files on the intranet. The process of reading and writing is much cleaner than using the earlier versions of ADO (2.0, 2.1). Since the Stream object can be opened without any arguments, the stream will be in memory and can be persisted to a file on the hard disk. This is a risky procedure as any program which gives access to writing files to the hard disk should be avoided, or used with caution. The IE browser has inherent support for ADODB.Stream objects, and as vulnerabilities in IE can be exploited, it needs to be disabled. Follow this link for a recipe to disable the support and use caution when saving files to hard disk. 

    Using the SaveToFile and LoadFile features (which were not discussed, but easy to implement) makes transferring binary files very easy. The argument for the ReadText() which reads everything has to be looked up in a reference or guessed, since intellisense gives no help. While reading text files, unless the charset is specified as ASCII, setting stream.type alone will not suffice.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This is a powerful procedure, but use it with caution. Any procedure that gives...
     

    DATABASE ARTICLES

    - Database Programming in C# with MySQL : Usin...
    - Formatting Techniques for Data Access from E...
    - Data Access from Excel VBA
    - Generating a Multiple Table Crystal Report u...
    - ADO and the Command Object
    - On Wiring Up an ADO Data Control
    - Reading and Writing to Files on the Intranet
    - Using ADO Record to Create and Navigate Intr...
    - Using Data Access Pages to Access Data on a ...
    - Using ADO with the SQL Native Client
    - ADO`s Stream Object
    - Opening a Record Object Referencing an Open ...
    - Introducing Jasper (SQL Anywhere 10 Beta)
    - Creating a Database Project in VS 2005
    - Manipulating ADO Recordsets




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway