File Object

class File extends Object

Provides an interface for file input/output, such as reading or writing text or retrieving its length. FileOpenは、このタイプのオブジェクトを返します。

"File"はクラスそのものなので、以下ではあらゆるFileオブジェクトのプレースホルダとして "FileObj"を使用します。

Objectから継承されたメソッドとプロパティに加えて、Fileオブジェクトは以下の定義済みのメソッドとプロパティを持ちます。

目次

メソッド

Read

ファイルから文字列を読み出し、ファイルポインタを進める。

String := FileObj.Read(Characters)

パラメータ

Characters

型:整数

省略された場合、ファイルの残りの部分が読み込まれ、1つの文字列として返されます。Otherwise, specify the maximum number of characters to read. Fileオブジェクトがコンソールバッファやパイプなどの非シーキングデバイスへのハンドルから作成された場合、このパラメータを省略すると、メソッドが失敗するか、現在利用可能なデータのみを返すことがあります。

戻り値

型:文字列

This method returns the string of characters that were read.

Write

文字列をファイルに書き込み、ファイルポインタを進める。

BytesWritten := FileObj.Write(String)

パラメータ

String

型:文字列

The string to write.

戻り値

型:整数

This method returns the number of bytes (not characters) that were written.

ReadLine

ファイルからテキストを1行読み出し、ファイルポインタを進める。

TextLine := FileObj.ReadLine()

戻り値

型:文字列

This method returns a line of text, excluding the line ending.

備考

最大65,534文字までの行を読み取ることができます。行の長さがこれを超える場合、それ以降このメソッドを呼び出すと、行の残りが返されます。

WriteLine

Writes a line of text to the file and advances the file pointer.

BytesWritten := FileObj.WriteLine(String)

パラメータ

String

型:文字列

If blank or omitted, an empty line will be written. Otherwise, specify the string to write, which is always followed by `n or `r`n, depending on the EOL flags used to open the file.

戻り値

型:整数

This method returns the number of bytes (not characters) that were written.

ReadNumType

ファイルから数値を読み出し、ファイルポインタを進める。

Num := FileObj.ReadNumType()

NumTypeは、UInt, Int, Int64, Short, UShort, Char, UChar, Double, Float のいずれかです。これらの型名は、DllCallと同じ意味を持ちます。

戻り値

型:Integer, Float or String (empty)

On success, this method returns a number. On failure, it returns an empty string.

備考

読み込んだバイト数が0でなく、NumTypeのサイズより小さい場合、不足するバイトは0とみなされます。

WriteNumType

ファイルに数値を書き込み、ファイルポインタを進める。

BytesWritten := FileObj.WriteNumType(Num)

NumTypeは、UInt, Int, Int64, Short, UShort, Char, UChar, Double, Float のいずれかです。これらの型名は、DllCallと同じ意味を持ちます。

パラメータ

Num

型:整数または浮動小数点数

The number to write.

戻り値

型:整数

This method returns the number of bytes that were written. For example, FileObj.WriteUInt(42) returns 4 if successful.

RawRead

ファイルから生のバイナリデータをメモリに読み込んで、ファイルポインタを進める。

BytesRead := FileObj.RawRead(Buffer , Bytes)

パラメータ

Buffer

型:オブジェクトまたは整数

データを受け取るBufferのようなオブジェクトまたはメモリアドレス。

Bufferへの読み込みを推奨します。Bytesが省略された場合、デフォルトでバッファのサイズになります。Bytesがバッファのサイズを超える場合は例外が発生します。

メモリアドレスを渡す場合は、Byteも指定する必要がある。

Bytes

型:整数

読み出す最大バイト数を指定します。Bufferがオブジェクトの場合はオプション、それ以外の場合は必須とします。

戻り値

型:整数

This method returns the number of bytes that were read.

RawWrite

ファイルに生のバイナリデータを書き込み、ファイルポインタを進める。

BytesWritten := FileObj.RawWrite(Data , Bytes)

パラメータ

Data

型:オブジェクト文字列整数のいずれか

バイナリデータを含むBufferのようなオブジェクトまたは文字列、またはメモリアドレス。オブジェクトまたは文字列が指定された場合、Bytesはオプションであり、デフォルトはバッファまたは文字列のサイズです。それ以外の場合は、Bytesも指定する必要があります。

Bytes

型:整数

書き込むバイト数を指定します。Dataがオブジェクトまたは文字列の場合はオプション、それ以外の場合は必須とします。

戻り値

型:整数

This method returns the number of bytes that were written.

Seek

ファイルポインタを移動します。

IsMoved := FileObj.Seek(Distance , Origin)

パラメータ

Distance

型:整数

移動する距離(バイト単位)。値が小さいほど、ファイルの先頭に近い。

Origin

型:整数

If omitted, it defaults to 2 when Distance is negative and 0 otherwise. Otherwise, specify one of the following numbers to indicate the starting point for the file pointer move:

  • 0 (SEEK_SET):ファイルの先頭です。距離は、0以上でなければならない。
  • 1 (SEEK_CUR):ファイルポインタの現在位置。
  • 2 (SEEK_END):ファイル終了。距離は、通常、マイナスになるはずです。

戻り値

型:整数(ブーリアン)

On success, this method returns 1 (true). On failure, it returns 0 (false).

備考

This method is equivalent to FileObj.Pos := Distance, if Distance is not negative and Origin is omitted or 0 (SEEK_SET).

Close

ファイルを閉じ、キャッシュ内のデータをディスクにフラッシュし、共有ロックを解除します。

FileObj.Close()

オブジェクトが解放されると自動的にファイルが閉じられますが、できるだけ早くファイルを閉じることをお勧めします。

属性

Pos

ファイルポインタの位置を取得または設定します。

CurrentPos := FileObj.Pos
FileObj.Pos := NewPos

CurrentPos and NewPos are a byte offset from the beginning of the file, where 0 is the first byte. ファイルにデータが書き込まれたり、ファイルから読み出されたりすると、ファイルポインタは自動的にそのデータの次のバイトに移動します。

This property is equivalent to FileObj.Seek(NewPos).

Length

ファイルサイズを取得または設定します。

CurrentSize := FileObj.Length
FileObj.Length := NewSize

CurrentSize and NewSize are the size of the file, in bytes.

このプロパティは、実際のファイルに対してのみ使用する必要があります。Fileオブジェクトがパイプへのハンドルから生成された場合、パイプの内部バッファで現在利用可能なデータ量を返すかもしれませんが、この動作は保証されていません。

AtEOF

Retrieves a non-zero number if the file pointer has reached the end of the file, otherwise zero.

IsAtEOF := FileObj.AtEOF

このプロパティは、実際のファイルに対してのみ使用する必要があります。Fileオブジェクトがコンソールバッファやパイプなどの非シーキングデバイスへのハンドルから作成された場合、そのようなデバイスは論理的に「ファイルの終わり」を持たないため、戻り値は意味をなさないかもしれません。

エンコード

このファイルオブジェクトが使用するテキストエンコーディングを取得または設定します。

CurrentEncoding := FileObj.Encoding
FileObj.Encoding := NewEncoding

NewEncodingには、数値のコードページ識別子(Microsoft Docsを参照)または以下の文字列のいずれかを指定します。

CurrentEncoding is one of the following strings:

CurrentEncoding is never a value with the -RAW suffix, regardless of how the file was opened or whether it contains a byte order mark (BOM). NewEncodingを設定しても、BOMが追加されたり削除されたりすることはありません。通常、BOMは最初にファイルを作成するときに書き込まれるためです。

NewEncodingUTF-8-RAWまたはUTF-16-RAWを指定することは有効ですが、-RAW という接尾辞は無視されます。これはFileObj.Encodingにのみ適用され、FileOpenには適用されません。

Handle

Returns a system file handle, intended for use with DllCall. See CreateFile.

Handle := FileObj.Handle

ファイルオブジェクトは、内部的に読み書きをバッファリングします。データがオブジェクトの内部バッファに書き込まれている場合、ハンドルが返される前にディスクにコミットされます。If the buffer contains data read from file, it is discarded and the actual file pointer is reset to the logical position indicated by the Pos property.