filedetails.cs

来自「csharp-solution,C#高效编程源码」· CS 代码 · 共 42 行

CS
42
字号

using System;                // Console
using System.IO;             // FileStream, FileReader
    
class FileDetails 
{
    static void Main(string[] args) 
	{
        string fileName = args[0];
        FileStream stream = new FileStream(fileName, FileMode.Open);
        StreamReader reader = new StreamReader(stream); 
        int size = (int)stream.Length;
        char[] contents = new char[size];
        for (int i = 0; i < size; i++) {
            contents[i] = (char)reader.Read();
        }
        reader.Close();
        Summarize(contents);
    }

    static void Summarize(char[] contents) 
	{
        int vowels = 0, consonants = 0, lines = 0;
        foreach (char current in contents) {
            if (Char.IsLetter(current)) {
                if ("AEIOUaeiou".IndexOf(current) != -1) {
                    vowels++;
                } else {
                    consonants++;
                }
            }
            else if (current == '\n') {
                lines++;
            }
        }
        Console.WriteLine("Total no of characters: {0}", contents.Length);
        Console.WriteLine("Total no of vowels    : {0}", vowels);
        Console.WriteLine("Total no of consonants: {0}", consonants);
        Console.WriteLine("Total no of lines     : {0}", lines);            
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?