delphi 读写文本

将字符串写入txt文档,读取txt文档中的内容。

//一次写字符串到文本文件,每次都会将原来的内容替换掉。
procedure FilePutContents(f,s:String);  // f为文件名,s为存的内容
var
	ss:TStrings;
begin
	ss:=TStringList.Create;
	ss.Text:=s;
	ss.SaveToFile(f, TEncoding.UTF8);
	ss.Free;
end;

下面为不替换原来的内容,追加写入文本:

procedure FilePutAssign(f,s: string);                   // 追加写入文本
var
	ss, txt:TStrings;
begin
	ss :=TStringList.Create;
	txt:=TStringList.Create;
	txt.LoadFromFile(f);
	ss.Text:= txt.Text + s;
	ss.SaveToFile(f, TEncoding.UTF8);
	ss.Free;
end;

从文本中读取文本:

//一次读取文本文件到字符串
function FileGetContents(f:String):String;
var
  ss:TStrings;
begin
  ss:=TStringList.Create;
  ss.LoadFromFile(f);
  result:=ss.Text;      // 返回值为文本中内容
  ss.Free;
end;
时间: 2024-08-28 08:58:45

delphi 读写文本的相关文章

C++ - 同步读写文本 代码(C++)

同步读写文本 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 写程序: 每个2秒写入文本一个数字; 读程序: 每个5秒读入文本最后一个数字; 写程序代码: #include <iostream> #include <fstream> #include <windows.h> using namespace std; int main (void) { ofstream ofs("D:/w.txt")

iOS 9应用开发教程之多行读写文本ios9文本视图

iOS 9应用开发教程之多行读写文本ios9文本视图 多行读写文本--ios9文本视图 文本视图也是输入控件,与文本框不同的是,文本视图可以让用户输入多行,如图2.23所示.在此图中字符串"说点什么吧"这一区域就是使用文本视图实现的,用户可以在此区域中写大量的文本内容.一般文本框视图使用UITextView实现. 图2.23  写日志 [示例2-9]以下将使用文本视图实现QQ中写说说并发表的功能.具体的操作步骤如下: (1)创建一个Single View Application模板类型

背水一战 Windows 10 (89) - 文件系统: 读写文本数据, 读写二进制数据, 读写流数据

[源码下载] 作者:webabcd 介绍背水一战 Windows 10 之 文件系统 读写文本数据 读写二进制数据 读写流数据 示例1.演示如何读写文本数据FileSystem/ReadWriteText.xaml <Page x:Class="Windows10.FileSystem.ReadWriteText" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x

delphi读写INI系统配置文件

delphi读写INI系统配置文件 一.调用delphi内建单元 uses System.IniFiles; 1.使用类TIniFile 2.类TIniFile的主要方法和函数: {$IFDEF MSWINDOWS}   { TIniFile - Encapsulates the Windows INI file interface (Get/SetPrivateProfileXXX functions) }   TIniFile = class(TCustomIniFile)   public

python_py2和py3读写文本区别

python2和python3的区别? python 2  str             对应      python3 bytes python 2 uincode            对应      pyhon3 str py2 字符串直接是2进制,unicode编码需要前面加上u py3 2进制需要前面加上 b 文本读写区别? py2 字符可以直接写到文件中,unicode需要编码,再写入文件,读文件,需要解码 py3 打开文件,指定编码格式自动编码,写入文件不需要编码和解码

读写文本数据

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 17.0px Helvetica; color: #29556f } 使用其他分隔符或行终止符打印 问题: 你想使用print() 函数输出数据,但是想改变默认的分隔符或者行尾符. 解决方案: 可以使用在print() 函数中使用sep 和end 关键字参数,以你想要的方式输出.比如: 1 #正常输出 2 print('dmeon', 89 , 8, 23) 3 4 #指定分隔符, 5 print('dm

零基础学python-3.7 还有一个程序 python读写文本

今天我们引入另外一个程序,文件的读写 我们先把简单的程序代码贴上.然后通过我们多次的改进.希望最后可以变成一个简单的文本编辑器 以下是我们最简单的代码: 'crudfile--读写文件' def readWholeFile(fileName): '读取整个文件' file = open(fileName, mode='r') text = [] for eachLine in file: print(eachLine) text.append(eachLine) return text def

Python之读写文本数据

知识点不多 普通操作 # rt 模式的 open() 函数读取文本文件 # wt 模式的 open() 函数清除覆盖掉原文件,write新文件 # at 模式的 open() 函数添加write新文件 with open("../../testData","rt",encoding="utf-8") as f : for line in f : print(line) # 写操作默认使用系统编码,可以通过调用 sys.getdefaultenco

delphi 读写文本文件(函数比较全)

需要两个按钮和两个Richedit控件,采用默认名称即可. procedure TForm1.Button1Click(Sender: TObject);  //写文件 var wText: TextFile;begin  AssignFile(wText, 'ip.txt');  Rewrite(wText);//创建文件,或者使用ReSet打开文件  Writeln(wText, richedit1.text);  CloseFile(wText);end; procedure TForm1