文本文件中的一行如下所示:
1056 Mark Supers Swagminator test32@gmail.com 3500
他们都必须进入自己的属性以获取新对象,然后下一行将进入他们自己的对象。怎么办呢?
这应该作为带有“帐户”的列表,这是帐户信息,名称等等。
答案 0 :(得分:0)
来自:Reading a text file word by word
using (var mappedFile1 = MemoryMappedFile.CreateFromFile(filePath))
{
using (Stream mmStream = mappedFile1.CreateViewStream())
{
using (StreamReader sr = new StreamReader(mmStream, ASCIIEncoding.ASCII))
{
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
string lineWords = line.Split(' ');
number = lineWords[0];
name = lineWords[1]; //and so on..
}
}
}
}
答案 1 :(得分:0)
你可以用Google搜索,我认为这很容易。但这是一个简单易行的方法。这些赞扬应该解释我在做什么。
// Create Class that holds the Attributes for you
// I am using auto properties here
// if you don't know what a property is pls google it - you need to know that
class AccountData
{
public int firstNumber { get; set; }
public string firstString { get; set; }
public string secondString { get; set; }
public string thirdString { get; set; }
public string mailAddress { get; set; }
public int lastNumber { get; set; }
}
List<AccountData> Parser(string FileLocationWithName)
{
// FileLocationWithName is something like "C:\MyFolder\MyFile.txt"
// If you want to write a backsplash (\) you need to write \\
// or you can use a @ bevore the string
// without @: "C:\\MyFolder\\MyFile.txt"
// with @: @"C:\MyFolder\MyFile.txt"
// Create your list
List<AccountData> resultList = new List<AccountData>();
// Oben a new FileStream - a StreamReader is good
using (StreamReader sr = new StreamReader(FileLocationWithName))
{
// Read the Whole file <=> sr is not at the end of the stream
while (!sr.EndOfStream)
{
// read a line and split it into the strings
string line = sr.ReadLine();
var elementsOfLine = line.Split(' ');
// create a new object of your accountData class and fill it with the string elements
var tempElement = new AccountData();
tempElement.firstNumber = int.Parse(elementsOfLine[0]);
tempElement.firstString = elementsOfLine[1];
tempElement.secondString = elementsOfLine[2];
tempElement.thirdString = elementsOfLine[3];
tempElement.mailAddress = elementsOfLine[4];
tempElement.lastNumber = int.Parse(elementsOfLine[5]);
resultList.Add(tempElement);
}
}
// return your list
return resultList;
}
答案 2 :(得分:0)
string line = "1056%Mark%Supers%Swagminator%test32@gmail.com%3500";
var propertyArray = line.Split('%');
foreach (string entry in propertyArray)
{
Console.WriteLine(entry);
}
打印每个条目。您可以将每个条目用作数组中的简单对象。
propertyArray[0]
将是1056
请注意尺寸。如果有人有4个姓名,您的电子邮件地址将从propertyArray[4]
移至propertyArray[5]