Handling Files in Delphi - Streams
(Page 4 of 4 )
Streams are the latest method used to access files on disk. The biggest advantage of streams is that it makes file access much faster and that it is interchangeable. For example, once you've read a file into a TFileStream variable, you can then go on to load its contents into a memo or into a Tmemory variable which is used to transfer files over the Internet. Here's an example of how to read the contents of a file:
var
fs:TFilestream ;
begin
fs:=tfilestream.create(Afilename,fmOpenRead);
try
memo.lines.loadfromstream(s);
finally
fs.free;
end;
end;
This example reads the file contents into a tfilestream variable, and then that stream is loaded into a memo. The fm in the fmOpenRead is short for "File Mode." There are other modes, such as:
fmCreate If the file exists, open for write access, otherwise, create a new file.
fmOpenRead Open for read access only.
fmOpenWrite Open for write access only.
fmOpenReadWrite Open for read and write access.
fmShareExclusive Read and write access is denied.
fmShareDenyWrite Write access is denied.
fmShareDenyRead Read access is denied. Do not use this mode in cross-platform applications.
fmShareDenyNone Allows full access for others.
There are many more ways to handle files with streams and Delphi has integrated this method into its core libraries. The tstream libraries do not differentiate between text, image and binary files.
| 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. |