FileOpen,FileRead,FileWrite [英] FileOpen, FileRead, FileWrite

查看:189
本文介绍了FileOpen,FileRead,FileWrite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何正确使用FileRead,FileWrite,缓冲区(或使用TFileStream)。

How to properly work with FileRead, FileWrite, buffers (or with TFileStream).

我需要读取一个整个文本文件到一个String,然后写回一个字符串到这个文件(用一个新的字符串替换)。

I need to read a whole text file to a String, then to write back a String to this file (replace it with a new string).

推荐答案

TStringList 如果您需要按照每行处理该文件,您需要什么。

TStringList is what you want if you need to deal with the file on a per-line basis.

如果您只想将其视为单个字符串blob,有 TStringStream

If you just want to treat it as a single string blob then there is TStringStream.

Stream := TStringStream.Create('', TEncoding.UTF8);
Try
  Stream.LoadFromFile('c:\desktop\in.txt');
  ShowMessage(Stream.DataString);

  Stream.Clear;
  Stream.WriteString('Greetings earthlings!');
  Stream.SaveToFile('c:\desktop\out.txt');
Finally
  Stream.Free;
End;

这篇关于FileOpen,FileRead,FileWrite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆