【从文件中查找汉字,并写入文件】

//做多语言时,将项目下所有文件中的汉字读出,去掉有注释的汉字,然后再写入单独的文件中

using System;
using System.Windows.Forms;
using System.IO;
using System.Text;

namespace 读出汉字
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
string path = Path.GetFullPath("../../data/");
ReadLine(GetFiles(path), ScanFile);

}

//读出js, cs, aspx, ascx 文件
private FileInfo[] GetFiles(string path)
{
DirectoryInfo dir = new DirectoryInfo(path);
string pattern = "*.js";
return dir.GetFiles(pattern, SearchOption.AllDirectories);
}

//按行读出每个文件的内容
private void ReadLine(FileInfo[] files, Action<FileInfo> action)
{
foreach (FileInfo f in files) action(f);
}

//对每一行扫描,在扫描过程中进行判断
private Action<FileInfo> ScanFile = (f) =>
{
string result = string.Empty;
StringBuilder sb = new StringBuilder(100);

StreamReader reader = f.OpenText(); //打开文件
string line = string.Empty;
while ((line = reader.ReadLine()) != null)
{
result = GetCh(line);

if (result.Length > 0) sb.AppendLine(result);
}

using (StreamWriter writer = new StreamWriter(Path.GetFullPath("../../data/") + f.Name + ".html"))
{
writer.WriteLine(sb);
}
};
//对字符串进行检查,判断汉字。 同时去掉注释
private static string GetCh(string s)
{

string result = string.Empty;

char[] arrChar = s.ToCharArray();
int i = 0;
foreach (char c in arrChar)
{
//if (c >= 65520 && c <= 65535) break;
if (c == 47 || c == 42)
{
i++;
if (i > 1) break;
}
if (c >= 0x4e00 && c <= 0x9fbb) result = string.Concat(result, c);

}

return result;

}
}
}

posted @ 2014-12-23 14:55  飞仙天外  阅读(69)  评论(0)    收藏  举报