散分,C#的速度的确快,原来用Ruby做同样的事(太慢)

redhatcn 2004-07-12 08:50:49
贴出源码,本人初学,不足之处敬请指出,谢谢!

//按照文件大小降序排,统计某个目录下的所有文件(递归方式)
using System;
using System.Collections;
using System.IO;
using System.Text;

class Test {

private static ArrayList al = new ArrayList();
private static string savePath = "d:/log.txt";
private static int FileCount = 0;
private static int DirCount = 0;

public static int Main(string[] args) {
string myDir = "";
Console.Write("请输入要列表统计的目录:");
myDir = Console.ReadLine().Trim();
if ( !myDir.EndsWith("/") ) {
myDir += "/";
}
if ( !Directory.Exists(myDir) ) {
goError("目录“ + myDir + ”不存在");
}

Console.WriteLine("正在分析数据...");
//把所有的文件和目录存储进ArrayList al中
ListDir(myDir);

//输出
PrintArrayList(myDir);

//释放一些变量
al = null;
savePath = null;

//结束
Console.WriteLine("按任意键结束...");
Console.ReadLine();
return 0;
}

//打印并存储
private static int PrintArrayList(string myDir) {
Console.Write("若要保存结果请输入文件名,否则请按回车键,按y代表:{0}:", savePath);

string readPath = Console.ReadLine().Trim().ToLower();
al.Sort(new MySort());
IEnumerator lem = al.GetEnumerator();

if ( !readPath.Equals("") ) {
if ( readPath != "y" ) {
savePath = readPath;
}
try {
StreamWriter sw = new StreamWriter(savePath, false, Encoding.Default);
sw.WriteLine("统计:" + myDir + " 结果如下\r\n---------------------");
while (lem.MoveNext()) {
sw.WriteLine(lem.Current);
Console.WriteLine(lem.Current);
}
sw.WriteLine("总共文件大小:" + Test.getSize(MyData.total));
sw.WriteLine("共有文件:" + Test.FileCount);
sw.WriteLine("共有目录:" + Test.DirCount);

//输出控制台
Console.WriteLine("统计:" + myDir + " 结果如下\r\n---------------------");
Console.WriteLine("总共文件大小:" + Test.getSize(MyData.total));
Console.WriteLine("共有文件:" + Test.FileCount);
Console.WriteLine("共有目录:" + Test.DirCount);

sw.Close();
sw = null;
} catch (Exception e) {
return goError("出现异常:" + e.ToString());
}
} else {
while (lem.MoveNext()) {
Console.WriteLine(lem.Current);
}
//输出控制台
Console.WriteLine("统计:" + myDir + " 结果如下\r\n---------------------");
Console.WriteLine("总共文件大小:" + Test.getSize(MyData.total));
Console.WriteLine("共有文件:" + Test.FileCount);
Console.WriteLine("共有目录:" + Test.DirCount);
}
return 0;
}

//递归存入列表“al”中
private static int ListDir(string thisDir) {

//这是一个隐藏系统的什么东东,读到这里的时候会出错,所以把它过滤了
if ( thisDir.EndsWith("System Volume Information/") ) {
return 0;
}

DirectoryInfo di = new DirectoryInfo(thisDir);
FileInfo[] files = di.GetFiles();
string[] dirs = Directory.GetDirectories(thisDir);

//循环文件
foreach ( FileInfo file in files ) {
Test.FileCount++;
al.Add(new MyData(thisDir + file, file.Length));
}

//循环目录
foreach ( string dir in dirs ) {
Test.DirCount++;
al.Add(new MyData(dir + "/", 0));
ListDir(dir + "/");
}
files = null;
dirs = null;
return 0;
}

//转换字节(byte, kb, mb, gb)
internal static string getSize(long sn) {
string size;
double n = Double.Parse(sn.ToString());
size = n.ToString() + " byte";

if ( n >= 1024 ) {
n /= 1024;
size = n.ToString() + " KB";
}

if ( n >= 1024 ) {
n /= 1024;
size = n.ToString() + " MB";
}

if ( n >= 1024 ) {
n /= 1024;
size = n.ToString() + " GB";
}

return size;
}

//输出错误信息
private static int goError(string errorStr) {
Console.WriteLine(errorStr);
return 1;
}
}

//实现IComparable中的CompareTo方法,为了ArrayList类的排序
class MyData : IComparable {
private string key;
private long val;
public static long total;

//构造器
public MyData(string key, long val) {
this.key = key;
this.val = val;
MyData.total += val;
}

//重载
public int CompareTo(object o) {
if ( o is MyData ) {
MyData tmp = (MyData) o;
return val.CompareTo(tmp.val);
}
throw new ArgumentException("错误的对象传递");
}

//重载ToString()方法
public override string ToString() {
return this.key + "\tSize: " + Test.getSize(this.val);
}

}

//重载IComparer中的Compare的方法,以便排序操作
class MySort : IComparer {
int IComparer.Compare(object x, object y) {
return Comparer.Default.Compare(y, x);
}
}
...全文
258 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
draclosta 2004-07-13
  • 打赏
  • 举报
回复
好东西,接分
redhatcn 2004-07-13
  • 打赏
  • 举报
回复
Ruby是一种类似Python\Perl之类的脚本语言,用C/C++开发的(可惜是日本人做的),完全面向对象,感觉有些地方和C#做的挺像

官方网站:http://www.ruby-lang.org

结贴!放分!


祝大家天天开心!
jockywin 2004-07-13
  • 打赏
  • 举报
回复
关注,学习
huaisha83 2004-07-13
  • 打赏
  • 举报
回复
接分
UP
weixing979 2004-07-13
  • 打赏
  • 举报
回复
接分,学习
lilionline 2004-07-12
  • 打赏
  • 举报
回复
我是农民。请问RUBY是什么?
KOF2004 2004-07-12
  • 打赏
  • 举报
回复
UP
timiil 2004-07-12
  • 打赏
  • 举报
回复
接分,BTW,弱弱地问,what is RUBY?
gkwww 2004-07-12
  • 打赏
  • 举报
回复

显示行号:
[工具]->[选项]->文本编辑器->C#->行号。

打上钩OK
chrch 2004-07-12
  • 打赏
  • 举报
回复
RUBY等动态语言速度是要慢一些,不过在国外很流行的
peterli1976 2004-07-12
  • 打赏
  • 举报
回复
接分
vitamines 2004-07-12
  • 打赏
  • 举报
回复
接分
tenglong2004 2004-07-12
  • 打赏
  • 举报
回复
同感,接分
  • 打赏
  • 举报
回复
恭喜!
ruby是动态语言吧?
redhatcn 2004-07-12
  • 打赏
  • 举报
回复
顺便问个问题,在Visual c# .NET 2003中怎么在代码编辑区中显示出前面的行号,像EditPlus那样?
匆匆岁月 2004-07-12
  • 打赏
  • 举报
回复
有时间慢慢盾了.

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧