FileOpen, FileRead, FileWrite



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

我需要将整个文本文件读取为字符串,然后将字符串写回该文件(用新字符串替换它)。

TStringList是您想要的,如果您需要按行处理文件。

如果你只想把它当作一个单独的字符串blob,那么就有TStringStream

Stream := TStringStream.Create('', TEncoding.UTF8);
Try
  Stream.LoadFromFile('c:desktopin.txt');
  ShowMessage(Stream.DataString);
  Stream.Clear;
  Stream.WriteString('Greetings earthlings!');
  Stream.SaveToFile('c:desktopout.txt');
Finally
  Stream.Free;
End;

将文件读取为字符串的最简单、最愚蠢的方法是使用TStringList,如下所示:

sl := TStringList.Create;
try
    sl.LoadFromFile('C:myfile.txt');
    //String can be read and modified using the Text property:
    oldString := sl.Text;
    sl.Text := 'foo bar';
    //Text can be written back to the file using:
    sl.WriteToFile('C:myfile.txt');
finally
    sl.Free;
end;

正如你的问题下面的评论中所指出的,这可能最终会改变字符串中的换行符——因此,根据你试图阅读/做什么,你需要注意这一点。

我的一些实用程序例程(您可以从我的网站上完整下载代码)。。。

//------------------------------------------------------------------------------
// CsiStrToBytes
//
// Convert pInStr to an array of bytes using the string encoding
// pStringEncoding (one of automatic, Ansi, UTF-16, or UTF-8) and optionally
// include the byte order mark according to the pIncludeBom flag
//------------------------------------------------------------------------------
function CsiStrToBytes(const pInStr: string;
                       pStringEncoding: TECsiStringEncoding;
                       pIncludeBom: Boolean): TByteDynArray;
var
{$IFDEF UNICODE}
  lStringEncoding: TECsiStringEncoding;
  lStringStream: TStringStream;
  lPreambleBytes: TBytes;
  lStringBytes: TBytes;
  lPreambleLen: Integer;
  lStringLen: Integer;
{$ENDIF}
  lLen: Integer;
{$IFDEF UNICODE}
  lIndex: Integer;
{$ENDIF}
begin
  if pInStr <> '' then begin
{$IFDEF UNICODE}
    if pStringEncoding = seAuto then
      lStringEncoding := CsiGetPreferredEncoding(pInStr)
    else
      lStringEncoding := pStringEncoding;
    // UTF-8 and UTF-16 encoding can be handled by the TStringStream class
    if (lStringEncoding = seUtf8) or (lStringEncoding = seUtf16) then begin
      if lStringEncoding = seUtf8 then
        lStringStream := TStringStream.Create(pInStr, TEncoding.Utf8)
      else
        lStringStream := TStringStream.Create(pInStr, TEncoding.Unicode);
      try
        // add the UTF-8 or UTF-16 byte order mark to the start of the array of
        // bytes if required
        if pIncludeBom then
          lPreambleBytes := lStringStream.Encoding.GetPreamble
        else
          SetLength(lPreambleBytes, 0);
        lStringBytes := lStringStream.Bytes;
        lPreambleLen := Length(lPreambleBytes);
        lStringLen := Length(lStringBytes);
        SetLength(Result, lPreambleLen + lStringLen);
        if lPreambleLen > 0 then
          Move(lPreambleBytes[0], Result[0], lPreambleLen);
        if lStringLen > 0 then
          Move(lStringBytes[0], Result[lPreambleLen], lStringLen);
      finally
        lStringStream.Free;
      end;
    end else begin
{$ENDIF}
      // Ansi encoding must be handled manually
      lLen := Length(pInStr);
      SetLength(Result, lLen);
{$IFDEF UNICODE}
      for lIndex := 1 to lLen do
        Result[lIndex - 1] := Ord(pInStr[lIndex]) and $00ff;
{$ELSE}
      Move(pInStr[1], Result[0], lLen);
{$ENDIF}
{$IFDEF UNICODE}
    end;
{$ENDIF}
  end else
    SetLength(Result, 0);
end;
//------------------------------------------------------------------------------
// CsiSaveToFile
//
// Saves pData, an array of bytes, to pFileName
//------------------------------------------------------------------------------
procedure CsiSaveToFile(const pData: TByteDynArray; const pFileName: string);
var
  lFileStream: TFileStream;
  lLen: Integer;
begin
  lFileStream := TFileStream.Create(pFileName, fmCreate);
  try
    lLen := Length(pData);
    if lLen > 0 then
      lFileStream.WriteBuffer(pData[0], lLen);
  finally
    lFileStream.Free;
  end;
end;
//------------------------------------------------------------------------------
// CsiSaveToFile
//
// Saves pText to pFileName using the string encoding pStringEncoding, which is
// one of automatic, Ansi, UTF-16, or UTF-8
//------------------------------------------------------------------------------
procedure CsiSaveToFile(const pText: string; const pFileName: string;
                        pStringEncoding: TECsiStringEncoding);
begin
  CsiSaveToFile(CsiStrToBytes(pText, pStringEncoding), pFileName);
end;

这里有两个函数可以执行您想要的操作:

function StringFromFile(const FileName: TFileName): RawByteString;
var F: THandle;
    Size: integer;
begin
  result := '';
  if FileName='' then
    exit;
  F := FileOpen(FileName,fmOpenRead or fmShareDenyNone);
  if PtrInt(F)>=0 then begin
{$ifdef LINUX}
    Size := FileSeek(F,0,soFromEnd);
    FileSeek(F,0,soFromBeginning);
{$else}
    Size := GetFileSize(F,nil);
{$endif}
    SetLength(result,Size);
    if FileRead(F,pointer(Result)^,Size)<>Size then
      result := '';
    FileClose(F);
  end;
end;
function FileFromString(const Content: RawByteString; const FileName: TFileName;
  FlushOnDisk: boolean=false): boolean;
var F: THandle;
    L: integer;
begin
  result := false;
  F := FileCreate(FileName);
  if PtrInt(F)<0 then
    exit;
  if pointer(Content)<>nil then
    L := FileWrite(F,pointer(Content)^,length(Content)) else
    L := 0;
  result := (L=length(Content));
{$ifdef MSWINDOWS}
  if FlushOnDisk then
    FlushFileBuffers(F);
{$endif}
  FileClose(F);
end;

它们使用低级的FileOpen/FIleSeek/FileRead/FileWrite函数。

您可以指定所需的任何fmShare*选项。

它使用RawByteString类型,因此希望以面向字节的方式处理文本。它不适用于Unicode文本文件,但适用于Ansi文本。如果您想使用Delphi2009以来的字符串类型与之交互,则必须设置适当的代码页。

在Delphi 2009之前,只需定义:

type
  RawByteString = AnsiString;

也添加了使用FileXXX函数族的请求示例。关于使用RawByteString和文件I/O的注意事项-它是完全有效的。另一个注意事项是:为了在底层代码中简洁起见,我省略了一些错误检查断言,以查找不太可能出现的错误。小心!

const
  FileName = 'Unit14.pas';  // this program is a stuntmaster,
                            // reads and writes its own source
procedure TForm14.FormClick(Sender: TObject);
var
  Stream: TFileStream;
  Buffer: RawByteString;
begin
  Stream := TFileStream.Create(FileName, fmOpenReadWrite or fmShareExclusive);
  // read entire file into string buffer
  SetLength(Buffer, Stream.Size);
  Stream.ReadBuffer(Buffer[1], Stream.Size);
  // do something with string
  OutputDebugString(PChar(Format('Buffer = "%s"', [Buffer])));
  // prepare to write
  Stream.Position := 0;   // rewind file pointer
  Stream.Size := 0;       // truncate the file
  // write entire string into the file
  Stream.WriteBuffer(Buffer[1], Length(Buffer));
  Stream.Free;
end;
// on right click - do exactly the same but using low-level FileXXX calls
procedure TForm14.FormContextPopup(Sender: TObject; MousePos: TPoint; var
    Handled: Boolean);
var
  Handle: Integer;
  Size: Cardinal;
  Buffer: RawByteString;
  Transferred: Integer;
begin
  Handle := FileOpen(FileName, fmOpenReadWrite or fmShareExclusive);
  Assert(Handle >= 0);
  // read entire file into string buffer
  Size := GetFileSize(Handle, nil);
  Assert(Size <> INVALID_FILE_SIZE);
  SetLength(Buffer, Size);
  Transferred := FileRead(Handle, Buffer[1], Size);
  Assert(not (Transferred < Size));
  // do something with string
  OutputDebugString(PChar(Format('Buffer = "%s"', [Buffer])));
  // prepare to write
  FileSeek(Handle, 0, 0); // rewind file pointer
  SetEndOfFile(Handle);   // truncate the file
  // write entire string into the file
  Transferred := FileWrite(Handle, Buffer[1], Length(Buffer));
  Assert(not (Transferred < Length(Buffer)));
  FileClose(Handle);
end;

相关内容

  • 没有找到相关文章

最新更新