如何将经典的Delphi字符串保存到磁盘(并阅读它们)? [英] How to save classic Delphi string to disk (and read them back)?

查看:140
本文介绍了如何将经典的Delphi字符串保存到磁盘(并阅读它们)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Delphi中实现非常基本的任务:将字符串保存到磁盘并将其加载回来。这似乎是微不足道的,但是自从升级到IOUtils(并在此之前更多的时间),这个TWICE已经出现了这个问题。这就是为什么我把辉煌的决定升级到IOUtils。)



我使用这样的东西:

 程序WriteToFile(CONST FileName:string; CONST uString:string; CONST WriteOp:WriteOperation); 
begin
如果WriteOp =(woOverwrite)
然后IOUtils.TFile.WriteAllText(FileName,uString)//覆盖
else IOUtils.TFile.AppendAllText(FileName,uString); // append
end;

简单的权利?什么可能出错?嗯,我最近走进了另一个在IOUtils的bug。所以,TFile是越野车。错误是详细的这里



任何人都可以分享一个不是基于IOUtils和的替代方法(或您的想法/想法)强>工作?嗯...上面的代码也为我工作了一段时间...所以,我知道如果难以保证一段代码(无论多少)将真正工作!



另外,我想要有我的WriteToFile过程可以将字符串保存到ANSI文件(uString只包含ANSI字符),否则为Unicode。

然后ReadAFile函数应该自动检测编码并正确读取字符串。

想法是仍然有文本编辑器会错误地打开/解释Unicode / UTF文件。所以,只要有可能,给用户一个好的旧的ANSI文本文件。



所以:

- 覆盖/附加

- 尽可能保存为ANSI

- 内存有效(加载文件时不要吃4GB的RAM)

- 应该与任何文本文件(最多2GB,显然)

- 没有IOUtils(太可怕的使用)

解决方案


然后ReadAFile函数应该自动检测编码并正确读取字符串。


这是不可能的。如果解释为任何文本编码,则存在格式良好的文件。例如,请参阅记事本文件编码问题,redux



这意味着您的目标是无法实现的,您需要更改它们。



我的建议是执行以下操作:




  • 选择一个编码, UTF-8,并坚持下去。

  • 如果该文件不存在,请创建该文件并向其中写入UTF-8字节。

  • 如果文件存在,打开它,寻找到最后,并附加UTF-8字节。



不了解UTF-8的文本编辑器不值得支持。如果您有意向,请在创建文件时加入UTF-8 BOM。使用 TEncoding.UTF8.GetBytes TEncoding.UTF8.GetString 进行编码和解码。


I want to achieve a very very basic task in Delphi: to save a string to disk and load it back. It seems trivial but I had problems doing this TWICE since I upgraded to IOUtils (and one more time before that... this is why I took the 'brilliant' decision to upgrade to IOUtils).

I use something like this:

procedure WriteToFile(CONST FileName: string; CONST uString: string; CONST WriteOp: WriteOperation);    
begin
   if WriteOp= (woOverwrite)
   then IOUtils.TFile.WriteAllText (FileName, uString)  //overwrite
   else IOUtils.TFile.AppendAllText(FileName, uString); //append
end;    

Simple right? What could go wrong? Well, I recently stepped into a (another) bug in IOUtils. So, TFile is buggy. The bug is detailed here.

Anyone has can share an alternative (or simply your thoughts/ideas) that is not based on IOUtils and it is known to work? Well... the code above also worked for a while for me... So, I know if difficult to guaranty that a piece of code (no matter how small) will really work!

Also I would REALLY like to have my WriteToFile procedure to save the string to an ANSI file when it is possible (the uString contains only ANSI chars) and as Unicode otherwise.
Then the ReadAFile function should automagically detect the encoding and correctly read the string back.
The idea is that there are still text editors out there that will wrongly open/interpret an Unicode/UTF file. So, whenever possible, give a good old ANSI text file to the user.

So:
- Overwrite/Append
- Save as ANSI when possible
- Memory efficient (don't eat 4GB of ram when the file to load is 2GB)
- Should work with any text file (up to 2GB, obviously)
- No IOUtils (too buggy to be of use)

解决方案

Then the ReadAFile function should automagically detect the encoding and correctly read the string back.

This is not possible. There exists files that are well-formed if interpreted as any text encoding. For instance see The Notepad file encoding problem, redux.

This means that your goals are unattainable and that you need to change them.

My advice is to do the following:

  • Pick a single encoding, UTF-8, and stick to it.
  • If the file does not exists, create it and write UTF-8 bytes to it.
  • If the file exists, open it, seek to the end, and append UTF-8 bytes.

A text editor that does not understand UTF-8 is not worth supporting. If you feel inclined, include a UTF-8 BOM when you create the file. Use TEncoding.UTF8.GetBytes and TEncoding.UTF8.GetString to encode and decode.

这篇关于如何将经典的Delphi字符串保存到磁盘(并阅读它们)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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