if FileExists('여기에 파일 패스와 이름을 넣고...') then begin
존재하면....^^
end else begin
존재하지 않을 경우...
end;
2. 텍스트 파일을 TStringList를 이용해 읽고/쓰기 예제 입니다. Delphi Tips에 있는 거 퍼 왔어요 참고 하세요!
============================================================
function LoadFile(const FileName: TFileName): string;
var
StringList: TStringList;
begin
StringList := TStringList.Create;
try
StringList.LoadFromFile(FileName);
Result := StringList.Text;
finally
StringList.Free;
end;
end;
텍스트 파일을 TFileStream를 이용해 읽고/쓰기
function LoadFile(const FileName: TFileName): string;
var
Stream: TFileStream;
begin
Stream := TFileStream.Create(FileName,
fmOpenRead or fmShareDenyWrite);
try
SetLength(Result, Stream.Size);
Stream.Read(Pointer(Result)^, Stream.Size);
finally
Stream.Free;
end;
end;
3. 그리고 델파이 Help를 보면 자세히 나와 있어요..
---------------------------------------------------------
The following example uses a button, a string grid, and a Save dialog box on a form. When the button is clicked, the user is prompted for a filename. When the user clicks OK, the contents of the string grid are written to the specified file. Additional information is also written to the file so that it can be read easily with the FileRead function.
procedure TForm1.Button1Click(Sender: TObject);
var
BackupName: string;
FileHandle: Integer;
StringLen: Integer;
X: Integer;
Y: Integer;
begin
if SaveDialog1.Execute then
begin
if FileExists(SaveDialog1.FileName) then
begin
BackupName := ExtractFileName(SaveDialog1.FileName);
BackupName := ChangeFileExt(BackupName, '.BAK');
if not RenameFile(SaveDialog1.FileName, BackupName) then
raise Exception.Create('Unable to create backup file.');
end;
FileHandle := FileCreate(SaveDialog1.FileName);
{ Write out the number of rows and columns in the grid. }
FileWrite(FileHandle,
StringGrid1.ColCount, SizeOf(StringGrid1.ColCount));
FileWrite(FileHandle,
StringGrid1.RowCount, SizeOf(StringGrid1.RowCount));
for X := 0 to StringGrid1.ColCount ?1 do
begin
for Y := 0 to StringGrid1.RowCount ?1 do
begin
{ Write out the length of each string, followed by the string itself. }
StringLen := Length(StringGrid1.Cells[X,Y]);
FileWrite(FileHandle, StringLen, SizeOf(StringLen));
FileWrite(FileHandle,
StringGrid1.Cells[X,Y], Length(StringGrid1.Cells[X,Y]);
end;
end;
FileClose(FileHandle);
end;
if FileExists('여기에 파일 패스와 이름을 넣고...') then begin
존재하면....^^
end else begin
존재하지 않을 경우...
end;
2. 텍스트 파일을 TStringList를 이용해 읽고/쓰기 예제 입니다. Delphi Tips에 있는 거 퍼 왔어요 참고 하세요!
============================================================
function LoadFile(const FileName: TFileName): string;
var
StringList: TStringList;
begin
StringList := TStringList.Create;
try
StringList.LoadFromFile(FileName);
Result := StringList.Text;
finally
StringList.Free;
end;
end;
procedure SaveFile(const FileName: TFileName; const S: String);
var
StringList: TStringList;
begin
StringList := TStringList.Create;
try
StringList.Text := S;
StringList.SaveToFile(FileName);
finally
StringList.Free;
end;
end;
텍스트 파일을 TFileStream를 이용해 읽고/쓰기
function LoadFile(const FileName: TFileName): string;
var
Stream: TFileStream;
begin
Stream := TFileStream.Create(FileName,
fmOpenRead or fmShareDenyWrite);
try
SetLength(Result, Stream.Size);
Stream.Read(Pointer(Result)^, Stream.Size);
finally
Stream.Free;
end;
end;
procedure SaveFile(const FileName: TFileName;
const S: string);
var
Stream: TFileStream;
begin
Stream := TFileStream.Create(FileName, fmCreate);
try
Stream.Write(Pointer(S)^, Length(S));
finally
Stream.Free;
end;
end;
======================================================
3. 그리고 델파이 Help를 보면 자세히 나와 있어요..
---------------------------------------------------------
The following example uses a button, a string grid, and a Save dialog box on a form. When the button is clicked, the user is prompted for a filename. When the user clicks OK, the contents of the string grid are written to the specified file. Additional information is also written to the file so that it can be read easily with the FileRead function.
procedure TForm1.Button1Click(Sender: TObject);
var
BackupName: string;
FileHandle: Integer;
StringLen: Integer;
X: Integer;
Y: Integer;
begin
if SaveDialog1.Execute then
begin
if FileExists(SaveDialog1.FileName) then
begin
BackupName := ExtractFileName(SaveDialog1.FileName);
BackupName := ChangeFileExt(BackupName, '.BAK');
if not RenameFile(SaveDialog1.FileName, BackupName) then
raise Exception.Create('Unable to create backup file.');
end;
FileHandle := FileCreate(SaveDialog1.FileName);
{ Write out the number of rows and columns in the grid. }
FileWrite(FileHandle,
StringGrid1.ColCount, SizeOf(StringGrid1.ColCount));
FileWrite(FileHandle,
StringGrid1.RowCount, SizeOf(StringGrid1.RowCount));
for X := 0 to StringGrid1.ColCount ?1 do
begin
for Y := 0 to StringGrid1.RowCount ?1 do
begin
{ Write out the length of each string, followed by the string itself. }
StringLen := Length(StringGrid1.Cells[X,Y]);
FileWrite(FileHandle, StringLen, SizeOf(StringLen));
FileWrite(FileHandle,
StringGrid1.Cells[X,Y], Length(StringGrid1.Cells[X,Y]);
end;
end;
FileClose(FileHandle);
end;
end;