.net System.IO(二)Textreader/Textwriter及其派生类:从流和字符串读取/写入字符

编程入门 行业动态 更新时间:2024-10-23 20:31:28

.net System.IO(二)Textreader/Textwriter及其派生类:从流和<a href=https://www.elefans.com/category/jswz/34/1771434.html style=字符串读取/写入字符"/>

.net System.IO(二)Textreader/Textwriter及其派生类:从流和字符串读取/写入字符

在最近得项目中,为了监控一些敏感违规得文字内容,涉及到了一个检测敏感词得一个功能吧。就需要字符串的读写,到这儿就不得不提起,TextReader、TextWriter、sStreamReader、StreamWriter、StringReader、StringWriter,这对夫妇以及4个孩纸~。
TextReader:读取有序字符系列的读取器,抽象类
TextWriter:编写有序字符系列的编写器,抽象类
这两个基本没啥好说的,我们经常用的是孩纸,
1、StreamReader、StreamWriter,一读一写(从流中进行读、写字符,派生类)

//filename,文件路径,例:C:\\私人\\test.txt(文件存在则覆盖,没有则创建)
StreamWriter sw = new StreamWriter(filename);
//将字符串写入流。 如果字符串为 null,则不写入任何内容
sw.Write("hello");//异步:WriteAsync
//将字符串写入该流,后跟行结束符, 如果字符串为 null,则只写入行终止符。
sw.WriteLine("\nworld");//异步:WriteLineAsync,换行写入:用\n或\r
sw.Dispose();StreamReader sr = new StreamReader(filename);
string temp = sr.ReadToEnd();//从当前位置读到结尾。 如果当前位置位于流结尾,则返回空字符串 (“”)。
//temp = sr.Read();//从当前位置,读取输入流中表示为 Int32 对象的下一个字符。重点:如果不再有可用的字符,则为 -1。
//temp = sr.ReadLine();//从当前位置,读取输入流中的下一行;如果到达了输入流的末尾,则为 null。
sr.Dispose();

2、StringReader、StringWriter(从字符串中进行读、写字符,派生类)

StringBuilder strBuild = new StringBuilder("hello");
StringWriter strw = new StringWriter(strBuild);
strw.Write(",world");//异步后面加Async
strw.WriteLine("\nworld");//异步后面加Async,换行写入:用\n或\r
strw.Dispose();
StringReader strr = new StringReader(strBuild.ToString());
Console.WriteLine("ReadLine=" + strr.ReadLine());//从当前位置读一行,没有返回null,而read()没有返回-1
Console.WriteLine("ReadToEnd=" + strr.ReadToEnd());//从当前位置读到尾
char[] blockArray = new char[5];
Console.WriteLine(strr.ReadBlock(blockArray,0,5));//从当前位置读取之指定数量的字符,blockArray,返回读取的字符
Console.WriteLine("array=" +string.Join(",", blockArray));
strr.Dispose();
Console.ReadKey();

所有的读,所指的当前位置,同一个流或字符,第一次读默认都是初始位置,第二次读是以上一次读的末尾作为初始位置

3、提供一个检验字符串是否包含敏感词的方法

/// <summary>
/// 检测是否包含敏感词
/// <param name="filename">存放敏感词的文件地址</param>
/// <param name="content">要进行检测的文本内容</param>
/// </summary>
public bool MonitorKeyword(string filename,string content)
{bool ismatch = false;try{if (!File.Exists(filename)){return ismatch;}List<string> list = new List<string>();StreamReader sr = new StreamReader(filename);string keywords = sr.ReadToEnd();sr.Dispose();//如果文件里没有敏感词,先用#占位,多以这里判断一下if (!string.IsNullOrEmpty(keywords) && !keywords.StartsWith("#")){string[] array = keywords.Split(new char[] { ',', '|' }, StringSplitOptions.RemoveEmptyEntries);foreach (string key in array){if(list.IndexOf(key) == -1){list.Add(key);}}}keywords = string.Join("|", list).Trim(new char[] { '|' });Regex regex = new Regex(keywords);ismatch = regex.IsMatch(content);}catch (Exception ex){//输出日志}return ismatch;
}

更多推荐

.net System.IO(二)Textreader/Textwriter及其派生类:从流和字符串读取/写入字符

本文发布于:2024-03-23 02:08:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1739185.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   字符   派生类   System   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!