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)】
查看全文
相关阅读:
三比三:无线监控优点与传输技术剖解
手把手教你学习FPGA系列视频教程_救护车鸣笛声
世纪大争论:Linux还是GNU/Linux?
[分享]我的LABVIEW快速开发串口测试软件实例
高能效系统的功耗优化技术
python学习小脚本多用户登录
python学习三级菜单省市选择
python学习购物车小程序
python学习语句总结
python学习猜年龄小游戏
原文地址:https://www.cnblogs.com/xieyunc/p/2793593.html
最新文章
内核中伙伴算法的实现
Linux添加系统调用的两种方法
内核创建进程的流程
内核中进程的执行流程
linux x86内核中的分页机制
浅析Linux内核调度
slab机制
1122 今天侃侃js的报错类型和符号
1123 原型模式来添加属性
1123 刚学的报错来了个实例
热门文章
1121 linux创建新用户
1122 接上一篇,js报错类型补充点具体的
!function()是干什么的?
进程和线程详解
get和post的区别
什么是WebService?
javascript小数相减出现一长串的小数位数
编程修养-C语言篇(二)
分享测试测量技术研讨会资料集锦,好的话请点赞
linux中如何使用微软鼠标的第4、5键
Copyright © 2011-2022 走看看