zoukankan
html css js c++ java
读写Unicode和UTF8格式文件
//---------------------------------------------- // Unit Name: FileExt.pas // Comment: 读写UTF8和UNICODE格式文件的一组函数 // Author: xieyunc 改自网络 // Date : 2007-11-23 10:47:59 //---------------------------------------------- unit FileExt; interface uses SysUtils,Windows,Types,Classes; const utf8Head: Array[0..2] of Char = (#239,#$bb,#$bf); unicodeHead: Array[0..1] of Char = (#255,#$fe); function ReadUtf8File(const filename:string):UTF8String; procedure WriteUtf8File(const filename:string;mem:Utf8String); function ReadUnicodeFile(const filename:string):WideString; procedure WriteUnicodeFile(const filename:string;mem:Widestring); implementation // read utf8 file function ReadUtf8File(const filename:string):Utf8String; var memoStream:TFileStream; buf:array of Char; begin memoStream:=TFileStream.Create(filename, fmOpenRead); try setlength(buf,memoStream.size); memoStream.ReadBuffer(buf[0], memoStream.size); //utf-8 file if (buf[0]=Utf8Head[0]) and (buf[1]=Utf8Head[1]) and (buf[2]=Utf8Head[2]) then begin FillChar(buf, SizeOf(buf), #0); memoStream.Seek(3,soFromBeginning); setlength(buf,memoStream.size-3); memoStream.ReadBuffer(buf[0], memoStream.size-3); result:=Utf8String(buf); exit; end else result:=UTF8Encode(WideString('文件不是UTF8格式!')); finally memoStream.Free; end; end; // read unicode file function ReadUnicodeFile(const filename:string):WideString; var memoStream:TFileStream; buf:array of Char; begin memoStream:=TFileStream.Create(filename, fmOpenRead); try setlength(buf,memoStream.size); memoStream.ReadBuffer(buf[0], memoStream.size); //unicode file if (buf[0]=UnicodeHead[0]) and (buf[1]=UnicodeHead[1]) then begin FillChar(buf, SizeOf(buf), #0); memoStream.Seek(2,soFromBeginning); setlength(buf,memoStream.size-2); memoStream.ReadBuffer(buf[0], memoStream.size-2); result:=PWideChar(buf); exit; end else result:=WideString('文件不是UNICODE格式!'); finally memoStream.Free; end; end; procedure WriteUnicodeFile(const filename:string;mem:Widestring); var wms:TMemoryStream; begin wms:=TMemoryStream.Create; wms.WriteBuffer(unicodeHead,length(unicodeHead)); wms.WriteBuffer(PChar(mem)^,length(mem)*2); wms.SaveToFile(filename); wms.Free; end; procedure WriteUtf8File(const filename:string;mem:Utf8String); var wms:TMemoryStream; begin wms:=TMemoryStream.Create; wms.WriteBuffer(utf8Head,length(utf8Head)); wms.WriteBuffer(pchar(mem)^,length(Ansistring(mem))); wms.SaveToFile(filename); wms.Free; end; end.
直接下载单元文件FileExt.pas
点击这里下载文件: FileExt.rar
谢祥选【小宇飞刀(xieyunc)】
查看全文
相关阅读:
Sql学习第二天——SQL DML与CTE解释
Sql学习第一天——SQL UNION 和 UNION ALL 操作符(1)
Sql学习第一天——SQL 练习题(3)
Sql学习第一天——SQL 将变量定义为Table类型(2)
C#性能优化实践(摘抄)
SQL数据纵横列查询
「SHOI2006」有色图
SP419/422 TRANSP(2) Transposing is Fun
ARC084F XorShift
洛谷 P4708 画画(无标号欧拉子图计数)
原文地址:https://www.cnblogs.com/xieyunc/p/2793593.html
最新文章
.net程序员应该掌握的常用类库 (二)
Oracle与sql Server对比
Oracle表空间设计理念
.net程序员应该掌握的常用类库
常用的数据库代码
Oracl 10G与sql server2005搜索运行时间测试
Oracle语句总结
FTP操作
.net程序员应该掌握的常用类库 (一)
.net程序员应该掌握的常用类库 (三)
热门文章
算法笔记:图论单源最短路
结论 (自己未曾学到的知识补充)
[USACO08OPEN]寻宝之路Clear And Present Danger
算法笔记:邻接表
简单查询树形结构数据库表
一条语句简单解决“每个Y的最新X”的SQL经典问题
配置Integration Services服务来解决:无法为此请求检索数据。(Microsoft.SqlServer.SmoEnum)
运行时动态设置并加密App.Config中的数据库连接字符串
将后台服务进程和窗口设置进程集成为一个程序
Oracle Data Convertion 简单方法
Copyright © 2011-2022 走看看