Created
May 31, 2014 17:42
-
-
Save yukkuribemani/6558906a07562607a1cf to your computer and use it in GitHub Desktop.
二次元Listジェネリックにファイル読み込み ref: http://qiita.com/yukkuribemani/items/62206eac33c31394a8df
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
名前 アドレス コメント | |
hamutaro [email protected] ハムタロサァンwww | |
hoge太郎 [email protected] hogehoge | |
null彦 [email protected] ヌルポ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment