判断文件编码并且替换指定字符串的方法

 1      private void Replace(string oldStr, string newStr, string file)
 2         {
 3             FileStream fs = File.OpenRead(file);
 4             //to know if this file is text file or binary file
 5             byte b;
 6             for (long i = 0; i < fs.Length; i++)
 7             {
 8                 b = (byte)fs.ReadByte();
 9                 if (b == 0)
10                 {
11                     MessageBox.Show("Not Text File");
12                     return;//if there is this byte then this is not text file。
13                 }
14             }
15             //Judge the type of encoding。
16             byte[] bytes = new byte[2];
17             Encoding coding = Encoding.Default;
18             if (fs.Read(bytes, 0, 2) > 2)
19             {
20                 if (bytes == new byte[2] { 0xFF, 0xFE }) coding = Encoding.Unicode;
21                 if (bytes == new byte[2] { 0xFE, 0xFF }) coding = Encoding.BigEndianUnicode;
22                 if (bytes == new byte[2] { 0xEF, 0xBB }) coding = Encoding.UTF8;
23             }
24             fs.Close();
25
26             string text = File.ReadAllText(file,coding);
27             File.WriteAllText(file, text.Replace(oldStr, newStr), coding);
28         }
时间: 2024-08-30 07:12:40

判断文件编码并且替换指定字符串的方法的相关文章

PHP替换指定字符串

在PHP中,有两个函数可以实现字符串替换,strtr()和str_repalce()函数. 首先我们简单了解下strtr()函数的定义及语法. strtr:转换指定字符. 两个语法: 第一种语法: string strtr( string $str, string $from, string $to) 第一个参数表示待转换的字符串.第二个参数表示字符串中与将要被转换的目的字符 to 相对应的源字符.第三个参数表示字符串中与将要被转换的字符 from 相对应的目的字符. 第二种语法: string

C#判断一个类中有无&quot;指定名称&quot;的方法

C#中可以通过反射分析元数据来解决这个问题,示例代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 using System; using System.Reflection; namespace Hello {     class Program     {  

javascript中通过replace函数搜索和替换指定字符串

javascript中我们可以通过replace函数替换部分字符串为指定字符串,本文展示了replace的详细用法,并且通过范例演示了如何进行部分替换.完整替换和不区分大小写替换. javascript中我们可以通过replace函数替换部分字符串为指定字符串.下面是replace函数的基本语法: str_var.replace("search_string", "replace_string") 下面看一个简单的范例: <script type="

js判断文件类型是否是指定格式

功能说明:js实现判断文件类型,图片‘视频等格式,当不符合格式时,会自动清除,并重新选择.’ 1..图片.视频等格式判断,直接上代码 <script type="text/javascript">//1.这个函数是,判断图片格式--------------------------------------------------------------------function checkImg(){var img_id=document.getElementById('m

判断文件是否存在的另一种方法 _access 和 _waccess

函数原型: int _access( const char *path, int mode ); int _waccess( const wchar_t *path, int mode ); 示例代码: [cpp] view plain copy #include <io.h> #include <stdio.h> #include <stdlib.h> int _tmain(int argc, _TCHAR* argv[]) { //如果文件具有指定的访问权限,则函数

Java遍历文件夹下所有文件并替换指定字符串

应用场景:比如有一个深层次的文件目录结构,如:javaAPI 每个文件里面都有相同的内容,而我们要统一修改为其他内容.上千个文件如果一个个修改显得太不明智. import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.PrintWriter; public class Test { /** *

Java基础知识强化之IO流笔记69:Properties练习之 判断文件中是否有指定的键,如果有就修改值的案例

1. 我有一个文本文件(user.txt),我知道数据是键值对形式的,但是不知道内容是什么. 请写一个程序判断是否有"lisi"这样的键存在,如果有就改变其值为"100"  分析:  A:把文件中的数据加载到集合中  B:遍历集合,获取得到每一个键  C:判断键是否有为"lisi"的,如果有就修改其值为"100"  D:把集合中的数据重新存储到文件中 2. 代码实现: 1 package cn.itcast_08; 2 3 i

js替换指定字符串

1 // var a = "212"; 2 // var b = []; 3 // for(var i=0;i<a.length;i++){ 4 // if(a[i]=="1"){ 5 // b[i]=3; 6 // }else{ 7 // b[i]= +a[i]; 8 // } 9 // } 10 // console.log(b); >>[2, 3, 2] 不兼容ie7及以下,因为不支持字符串[]形式.

C# 判断文件编码

无耐网上各种方法都有缺陷,此方法为原创,暂问发现问题.如发现请指正 public static Encoding GetFileEncodingByContent(string path) { var contentWithUTF8 = File.ReadAllText(path, Encoding.UTF8); var contentWithGBK = File.ReadAllText(path, Encoding.GetEncoding("GBK")); if (contentWi