【黑马程序员】File.ReadAllLines(错误)

来源:互联网 发布:android app 端口号 编辑:程序博客网 时间:2024/06/15 05:06

------- WindowsPhone 7手机开发.Net培训、期待与您交流! -------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace test2
{
    class Program
    {
        // 2.文本文件中存储了多个文章标题、作者,标题和作者之间用若干空格(数量不定)隔开,每行一个,标题有的长有的短,输出到控制台的时候最多标题长度10,
        //如果超过10,则截取长度8的子串并且最后添加“...”,加一个竖线后输出作者的名字。
        static void Main(string[] args)
        {
            string[] readText = File.ReadAllLines("1.txt", Encoding.Default);
            foreach (string line in readText)
            {
                string[] str = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                string str1;
                if (str[0].Length > 10)
                {
                    str1 = str[0].Substring(0, 8) + "...";
                }
                else
                {
                    str1 = str[0] + "|";
                }
                string title = str[0];
                string author = str[1];
                Console.WriteLine("标题:{0},作者:{1}", title, author);
            }
            Console.ReadKey();
        }
    }
}

 

 

哪里出错了呢?1.txt在Debug文件下。

原创粉丝点击