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)】
查看全文
相关阅读:
广东省第三届普通高中信息技术优质课交流评选活动参后感
预说课
arraylist和list的区别
List 泛型类 详解
C#中const和readonly的区别
PHP array_walk() 函数
栈,堆,代码区,全局(静态)区 ,常量区
PHP_SELF、 SCRIPT_NAME、 REQUEST_URI区别
c#中Dictionary、ArrayList、Hashtable和数组的区别(
PHP文件包含语句 include、include_once、require、require_once
原文地址:https://www.cnblogs.com/xieyunc/p/9126598.html
最新文章
Mileage Stats highlevel architecture
MySQL安装出现的问题:the security settings could not be applied to 。。。 原因是原来的东西没有卸载干净
书写数学公式插件 LaTeXiT
Finance 网站收集
MVC 3.0 MicrosoftAjax 的使用
Create a DLL by CSharp or VB.Net for VBA摘自网络
第十节 10作业点评加法计算器 简单
第十节 5ASP.Net的IsPostBack揭秘 简单
第十节 4基于ashx方式的ASP.Net开发 简单
第十节 7ASP.Net揭秘之Input版自增 简单
热门文章
第十节 15ASP.Net中使用Cookie 简单
第十节 8ASP.Net揭秘之Input版自增补充说明 简单
第十节 6Get和Post的区别 简单
第十节 3网站和WebApplication的区别 简单
第十节 2ASP.Net服务端控件做了什么 简单
第十节 12ViewState 简单
linux grep命令
看《无极》
信息技术组2006元旦晚会绝技
又一个不眠之夜
Copyright © 2011-2022 走看看