从具有多个分隔符的文本文件中读取并组织到对象中

时间:2014-03-06 19:07:23

标签: c# delimiter

我有一个使用|的文本文件和“-----”作为分隔符。我想知道如何处理这个问题。我有一个主类,还有3个类,Song.cs,Movie.cs和Book.cs.我需要能够从文本文件中读取,并将每种媒体类型与媒体的标题,作者,年份和描述一起分类到正确的对象中(描述在rot13中加密,以防人们想知道,我已经知道如何解密)。文本示例和代码如下。谢谢你的时间。

Textfile示例:

BOOK|The Fellowship of the Ring|1954|J.R.R. Tolkien
Fnheba, gur Qnex Ybeq, unf tngurerq gb uvz nyy gur Evatf bs Cbjre rkprcg bar - gur Bar Evat gung ehyrf gurz nyy - juvpu unf snyyra vagb gur unaqf bs gur uboovg Ovyob Onttvaf. Lbhat Sebqb Onttvaf svaqf uvzfrys snprq jvgu na vzzrafr gnfx jura Ovyob ragehfgf gur Evat gb uvf pner. Sebqb zhfg znxr n crevybhf wbhearl npebff Zvqqyr-rnegu gb gur Penpxf bs Qbbz, gurer gb qrfgebl gur Evat naq sbvy gur Qnex Ybeq va uvf rivy checbfr.
-----
SONG|Fly Me to the Moon|1964|It Might as Well be Swing|Frank Sinatra
-----
MOVIE|The Princess Bride|1987|Rob Reiner
N xvaqyl tenaqsngure fvgf qbja jvgu uvf tenaqfba naq ernqf uvz n orqgvzr fgbel. Gur fgbel vf bar gung unf orra cnffrq qbja guebhtu sebz sngure gb fba sbe trarengvbaf. Nf gur tenaqsngure ernqf gur fgbel, gur npgvba pbzrf nyvir. Gur fgbel vf n pynffvp gnyr bs ybir naq nqiragher nf gur ornhgvshy Ohggrephc vf xvqanccrq naq uryq ntnvafg ure jvyy va beqre gb zneel gur bqvbhf Cevapr Uhzcreqvapx, naq Jrfgyrl (ure puvyqubbq ornh, abj erghearq nf gur Qernq Cvengr Eboregf) nggrzcgf gb fnir ure. Ba gur jnl ur zrrgf na nppbzcyvfurq fjbeqfzna naq n uhtr, fhcre fgebat tvnag, obgu bs jubz orpbzr uvf pbzcnavbaf va uvf dhrfg. Gurl zrrg n srj onq thlf nybat gur jnl gb erfphr Ohggrephc.
-----
BOOK|The Two Towers|1954|J.R.R. Tolkien
Gur Sryybjfuvc jnf fpnggrerq. Fbzr jrer oenpvat ubcryrffyl sbe jne ntnvafg gur napvrag rivy bs Fnheba. Fbzr jrer pbagraqvat jvgu gur gernpurel bs gur jvmneq Fnehzna. Bayl Sebqb naq Fnz jrer yrsg gb gnxr gur npphefrq Evat bs Cbjre gb or qrfgeblrq va Zbeqbe - gur qnex Xvatqbz jurer Fnheba jnf fhcerzr. Gurve thvqr jnf Tbyyhz, qrprvgshy naq yhfgsvyyrq, fynir gb gur pbeehcgvba bs gur Evat.
-----
BOOK|The Return of the King|1955|J.R.R. Tolkien
Nf gur Funqbj bs Zbeqbe tebjf npebff gur ynaq, Nentbea vf erirnyrq nf gur uvqqra urve bs gur napvrag xvatf. Tnaqnys zvenphybhfyl ergheaf naq qrsrngf gur rivy jvmneq, Fnehzna. Fnz yrnirf uvf znfgre sbe qrnq nsgre n onggyr jvgu gur tvnag fcvqre, Furybo; ohg Sebqb vf fgvyy nyvir - va gur unaqf bs gur Bepf. Naq nyy gur juvyr gur nezvrf bs gur Qnex Ybeq ner znffvat - naq gur Bar Evat pbzrf rire pybfre gb gur Penpxf bs Qbbz.
-----

class Program
{
    private const int MAX_MEDIA_OBJECTS = 100; // Max number of array objects
    private int numberOfMedia = 0; // Counter to keep track of amount
                                   // of media in Data.txt

    private Media[] media = new Media[MAX_MEDIA_OBJECTS];

    static void Main(string[] args)
    {
    }

    public void ReadData()
    {
        // Opens the Data.txt file for read access
        FileStream mediaFile =
            new FileStream("Data.txt", FileMode.Open, FileAccess.Read);
        StreamReader mediaData = new StreamReader(mediaFile);

        string mediaRow; // Holds each media data per row

        while ((mediaRow = mediaData.ReadLine()) != null)
        {
            string[] mediaDataSplit = mediaRow.Split('|');
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您可以阅读文件并根据分隔符-----拆分它以获取行,然后使用|分隔符再次拆分以获取每行中的单词。

试试这个:

string path=@"C:\Data.txt";
var allText = File.ReadAllText(path);
string [] allLines=allText.Split(new[] { "-----" }, 
                     StringSplitOptions.RemoveEmptyEntries);
foreach (var line in allLines)
{
string[] allWords = line.Split(new[] { "|" }, 
                     StringSplitOptions.RemoveEmptyEntries);

 /*do some thing here*/

 /*do some thing here*/
}

答案 1 :(得分:2)

首先,我将使三个类继承自基类Media。每个子类都将实现一个方法parseMediaInfo(String[] elements),它负责解析 - 或者如果它对所有人都相同,则在基类中实现它。然后实现以下内容:

public void ReadData()
{
    // Opens the Data.txt file for read access
    FileStream mediaFile = new FileStream("Data.txt", FileMode.Open, FileAccess.Read);
    StreamReader mediaData = new StreamReader(mediaFile);

    string mediaRow; // Holds each media data per row
    Media media = null;
    var list = new List<Media>();
    while ((mediaRow = mediaData.ReadLine()) != null)
    {
        if (mediaRow.startsWith("-----") && media != null) {
            list.add(media);
            media = null;
        }
        else 
        {
            if (media != null) media.parseDescription(mediaRow);
            else {
                string[] mediaDataSplit = mediaRow.Split('|');
                media = constructMediaOfType(mediaDataSplit[0]);
                media.parseMediaInfo(mediaDataSplit);        
            }               
        }           
    }
}

private static Media constructMediaOfType(String type) 
{
    switch (type) {
        case "BOOK": return new Book();
        case "SONG": return new Song();
        case "MOVIE" return new Movie();
        default: throw new NotImplementedException();
    }
}