From Clomosy Docs
TclFileStream enables applications to read from and write to a file on disk.
Use TclFileStream to access the information in disk files. TclFileStream will open a named file and provide methods to read from or write to it.
Pros:
- Large Data Processing: It can handle very large data sizes, limited only by disk space.
- Persistence: Data is persistent and remains available even after the application is closed.
Cons:
- Slower Access: File I/O operations are generally slower than memory operations due to disk access delays.
- Disk I/O: Involves reading from and writing to disk, which can be slower and resource-intensive.
Feature | Use of | Definition |
---|---|---|
TclFileStream | FileStream: TCLFileStream; | A variable belonging to the TclFileStream class is created. |
TCLFileStream.Create(filePath, fmCreate); | FileStream = TCLFileStream.Create(clPathCombine('ok.png',Clomosy.AppFilesPath), fmCreate); //fmOpenRead - fmOpenWrite - fmOpenReadWrite - fmExclusive - fmShareExclusive - fmShareDenyWrite - fmShareDenyNone - fmCreate - fmClosed - fmInput - fmOutput - fmInOut | When creating a new TclFileStream object on the form, it takes two parameters. The first parameter specifies the file path, and the second parameter indicates how the file will be opened. In the example, it creates the specified file and opens it in writing mode (fmCreate). |
AsBase64 | noteMemo.Lines.Text = FileStream.AsBase64; //noteMemo //TclMemo | Returns base64 string data. |
Free | FileStream.Free; | It releases the memory occupied and destroys the object. |
Example
This example creates a form and adds an image, a text area, and a button onto it. A procedure named FileStreamExample first saves the image to a file (in fmCreate mode), then loads the same image from the file (in fmOpenRead mode), and finally places the Base64 code of the image into a text area.
var Form1:TCLForm; SourceImg : TclImage; noteMemo : TclMemo; btn1 : TclButton; void FileStreamExample; var FileStream: TCLFileStream; { // Save Img To File FileStream = TCLFileStream.Create(clPathCombine('ok.png',Clomosy.AppFilesPath), fmCreate); try SourceImg.Bitmap.SaveToStream(FileStream); finally FileStream.Free; } // Load Img From File FileStream = TCLFileStream.Create(clPathCombine('ok.png',Clomosy.AppFilesPath), fmOpenRead); try SourceImg.Bitmap.LoadFromStream(FileStream); noteMemo.Lines.Text = FileStream.AsBase64; finally FileStream.Free; } } { Form1 = TCLForm.Create(Self); Form1.AddAssetFromUrl('https://clomosy.com/educa/ok.png'); noteMemo = Form1.AddNewMemo(Form1,'noteMemo','---'); noteMemo.Align = alMostTop; noteMemo.Height = 200; noteMemo.StyledSettings = ssFamily; noteMemo.TextSettings.WordWrap = True; SourceImg = Form1.AddNewImage(Form1,'SourceImg'); SourceImg.Align = alClient; Form1.setImage(SourceImg,'https://clomosy.com/educa/hint.png'); btn1 = Form1.AddNewButton(Form1,'btn1','Upload'); btn1.StyledSettings = ssFamily; btn1.TextSettings.Font.Size = 20; btn1.TextSettings.FontColor = clAlphaColor.clHexToColor('#8a067c'); btn1.Align = alBottom; Form1.AddNewEvent(btn1,tbeOnClick,'FileStreamExample'); Form1.Run; }