를 얻을; 이 코드를 분리하여 코드 이 입력 된 레코드로 텍스트를 구문 분석 한 다음 유형이 지정된 데이터 만 처리해야하는 경우 업스트림을 처리합니다.
예를 들어 (여기서는 파일이 탭으로 구분되어 있다고 가정하고 있지만 쉽게 색인 생성 된 열로 변경할 수 있습니다.); .NET 2.0 (및 아마에서 작동 버전 (아주 우아하지만, 작동하지 않는)을
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
static class Program
{
static void Main()
{
foreach (var item in ReadFile("my.txt").OrderBy(x => x.Joined))
{
Console.WriteLine(item.Names);
}
}
static readonly char[] tab = { '\t' };
class Foo
{
public string Names { get; set; }
public int Age { get; set; }
public string Designation { get; set; }
public DateTime Joined { get; set; }
}
static IEnumerable<Foo> ReadFile(string path)
{
using (var reader = File.OpenText(path))
{
// skip the first line (headers), or exit
if (reader.ReadLine() == null) yield break;
// read each line
string line;
var culture = CultureInfo.InvariantCulture;
while ((line = reader.ReadLine()) != null)
{
var parts = line.Split(tab);
yield return new Foo
{
Names = parts[0],
Age = int.Parse(parts[1], culture),
Designation = parts[2],
Joined = DateTime.Parse(parts[3], culture)
};
}
}
}
}
을 그리고 여기 : 어떻게 약간의 작업을보고 내 Main
방법은 데이터로 작업 할 필요 .NET 1.1)에서만 ISO-1 언어 기능을 사용합니다. 개인적으로 .NET 1.1을 사용하는 것은 어리석은 일이며 .NET 2.0을 사용하는 경우 List<T>
이 ArrayList
보다 훨씬 더 좋습니다.
using System;
using System.Collections;
using System.Globalization;
using System.IO;
class Program
{
static void Main()
{
ArrayList items = ReadFile("my.txt");
items.Sort(FooByDateComparer.Default);
foreach (Foo item in items)
{
Console.WriteLine(item.Names);
}
}
class FooByDateComparer : IComparer
{
public static readonly FooByDateComparer Default
= new FooByDateComparer();
private FooByDateComparer() { }
public int Compare(object x, object y)
{
return ((Foo)x).Joined.CompareTo(((Foo)y).Joined);
}
}
static readonly char[] tab = { '\t' };
class Foo
{
private string names, designation;
private int age;
private DateTime joined;
public string Names { get { return names; } set { names = value; } }
public int Age { get { return age; } set { age = value; } }
public string Designation { get { return designation; } set { designation = value; } }
public DateTime Joined { get { return joined; } set { joined = value; } }
}
static ArrayList ReadFile(string path)
{
ArrayList items = new ArrayList();
using (StreamReader reader = File.OpenText(path))
{
// skip the first line (headers), or exit
if (reader.ReadLine() == null) return items;
// read each line
string line;
CultureInfo culture = CultureInfo.InvariantCulture;
while ((line = reader.ReadLine()) != null)
{
string[] parts = line.Split(tab);
Foo foo = new Foo();
foo.Names = parts[0];
foo.Age = int.Parse(parts[1], culture);
foo.Designation = parts[2];
foo.Joined = DateTime.Parse(parts[3], culture);
items.Add(foo);
}
}
return items;
}
}
왜 오늘날에는 모든 arraylist를 사용합니까? 'File [? readAllLines'] 대신'String []'을 반환하는 것이 어떨까요? 더 좋게는'CustomClass'가 위의 속성을 가지고있는'List'를 적재하십시오. –
텍스트 파일의 "열"이 구분되어 있습니까 (예 : 탭)? –
또한 열거자를 직접 사용하지 마시고 foreach를 대신 사용하십시오 :'foreach (items in items) Console.WriteLine (item);' – digEmAll