资源简介
C#的读写问题中数据流的操作中的一个必备的案例,也是一个必备的了解
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
namespace ConsoleApplication1
{
class Entry
{
private string strWord;
public string Word
{
get { return strWord; }
}
private int iFrequence;
public int Frequence
{
get { return iFrequence; }
}
public Entry(string word)
{
this.strWord = word;
this.iFrequence = 1;
}
public Entry(string a int b)
{
this.strWord = a;
this.iFrequence = b;
}
public void IncFreq()
{
this.iFrequence++;
}
}
class FileStat
{
public static void Run(string fileHTML string fileStore)
{
int k = 0;
if (!File.Exists(fileHTML))
{
new FileNotFoundException(“无法打开文件 [“ + fileHTML + “]!“);
}
StreamReader reader = new StreamReader(fileHTML);
ArrayList list = new ArrayList();
ArrayList list1 = new ArrayList();
string line;
line = reader.ReadLine();
while (line != null)
{
//Skip empty line
if (!line.Equals(““))
{
string[] splits = line.Split(new char[] { ‘ ‘ ‘\t‘ ‘‘ ‘.‘ ‘?‘ ‘!‘ ‘<‘ ‘>‘ ‘\\‘ ‘/‘ ‘:‘ });
foreach (string str in splits)
{
if (!str.Equals(““))
{
bool found = false;
foreach (Entry entry in list)
{
if (entry.Word.Equals(str StringComparison.OrdinalIgnoreCase))
{
entry.IncFreq();
found = true;
break;
}
}
if (found == false)
{
list.Add(new Entry(str));
k++;
}
}
}
}
line = reader.ReadLine();
}
reader.Close();
string[] keyarray = new string[list.Count];
int[] valuearray = new int[list.Count];
int i = 0;
foreach (Entry skey in list)
{
keyarray[i] = Convert.ToString(skey.Word); //赋值
valuearray[i] = Convert.ToInt32(skey.Frequence);//赋值
i++;
}
for (int a = 0; a < list.Count; a++) //排序
{
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 34 2008-09-14 15:29 ConsoleApplication1\ConsoleApplication1\bin\Debug\a
文件 20480 2008-09-15 21:57 ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe
文件 17920 2008-09-15 21:57 ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.pdb
文件 5632 2005-11-11 22:25 ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe
文件 2026 2008-09-14 19:16 ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.csproj
文件 182 2008-09-15 22:14 ConsoleApplication1\ConsoleApplication1\obj\ConsoleApplication1.csproj.FileList.txt
文件 20480 2008-09-15 21:57 ConsoleApplication1\ConsoleApplication1\obj\Debug\ConsoleApplication1.exe
文件 17920 2008-09-15 21:57 ConsoleApplication1\ConsoleApplication1\obj\Debug\ConsoleApplication1.pdb
文件 4709 2008-09-15 22:18 ConsoleApplication1\ConsoleApplication1\Program.cs
文件 1209 2008-09-14 15:20 ConsoleApplication1\ConsoleApplication1\Properties\AssemblyInfo.cs
文件 946 2008-09-14 15:20 ConsoleApplication1\ConsoleApplication1.sln
..A..H. 10240 2008-09-15 22:18 ConsoleApplication1\ConsoleApplication1.suo
目录 0 2008-09-14 15:20 ConsoleApplication1\ConsoleApplication1\obj\Debug\TempPE
目录 0 2008-09-14 15:28 ConsoleApplication1\ConsoleApplication1\bin\Debug
目录 0 2008-09-15 21:57 ConsoleApplication1\ConsoleApplication1\obj\Debug
目录 0 2008-09-14 19:14 ConsoleApplication1\ConsoleApplication1\bin
目录 0 2008-09-14 15:20 ConsoleApplication1\ConsoleApplication1\obj
目录 0 2008-09-14 15:20 ConsoleApplication1\ConsoleApplication1\Properties
目录 0 2008-09-15 22:18 ConsoleApplication1\ConsoleApplication1
目录 0 2008-09-14 15:20 ConsoleApplication1
----------- --------- ---------- ----- ----
101778 20
- 上一篇:Wpf 加载 Cur资源
- 下一篇:C#源代码 读取多个txt写入excel
评论
共有 条评论