Skip to content

Instantly share code, notes, and snippets.

@yukkuribemani
Created May 31, 2014 17:42
Show Gist options
  • Save yukkuribemani/6558906a07562607a1cf to your computer and use it in GitHub Desktop.
Save yukkuribemani/6558906a07562607a1cf to your computer and use it in GitHub Desktop.
二次元Listジェネリックにファイル読み込み ref: http://qiita.com/yukkuribemani/items/62206eac33c31394a8df
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace test
{
static class FileLoad
{
public static List<List<string>> load(string filename)
{
List<List<string>> LoadFileData = new List<List<string>>();
string filepath = "テキストファイルがあるパス";
filepath += filename;
try
{
using (StreamReader sr = new StreamReader(filepath, Encoding.GetEncoding("utf-8")))
{
//sr.ReadLine(); 最初の一行分(表のヘッダ部分)を飛ばしたい場合
while (!sr.EndOfStream)
{
List<string> addData = new List<string>();
string line = sr.ReadLine();//一行ずつ読み込む
string[] splitData = line.Split('\t');//タブ区切りで分割したものを配列に追加
for (int i = 0; i < splitData.Length; i++)
{
addData.Add(splitData[i]);//追加用のList<string>の作成
}
addData.Add("\n");
LoadFileData.Add(addData);//List<List<string>>のList<string>部分の追加
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return LoadFileData;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace test
{
class Program
{
static void Main(string[] args)
{
List<List<string>> loadData = (FileLoad.load("sample.txt"));
for (int i = 0; i < loadData.Count; i++)//ジェネリックの出力
{
foreach (string data in loadData[i])
{
Console.Write(data);
}
}
Console.ReadKey();
}
}
}
名前 アドレス コメント
hamutaro koushi@example.com ハムタロサァンwww
hoge太郎 hogehoge@example.com hogehoge
null彦 nullnull@example.com ヌルポ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment