- 開発技術
C#でのテキストファイルの読み込みについて
- その他

テキストファイルを読み込むには
【エンジニア募集中】フルリモート可◎、売上/従業員数9年連続UP、平均残業8時間、有給取得率90%、年休124日以上 etc. 詳細はこちらから>
テキストファイルを読み込む方法について。
StreamReaderクラスを使う方法が、基本的な方法になります。
StreamReaderクラスを使う際、Openメソッドを記載する必要はありません。
下記の場合、readme.txtを開いています。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using System; using System.IO; using System.Text; class FileRead {   static void Main() {     StreamReader streamReader = new StreamReader(         "readme.txt", Encoding.GetEncoding("Shift_JIS"));     string str = streamReader.ReadToEnd();     streamReader.Close();     Console.Write(str);   } } | 
ファイルを読み込む
ファイルの読み込み方には様々なパターンが存在します。
①1度にすべて読み込む
ReadToEndメソッドを使用すると、内容をすべて読み込むことができます。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using System; using System.IO; using System.Text; class FileRead {   static void Main() {     StreamReader streamReader = new StreamReader(         "readme.txt", Encoding.GetEncoding("Shift_JIS"));     string str = streamReader.ReadToEnd();     streamReader.Close();     Console.Write(str);   } } | 
②一行ずつ読み込む
ReadLineメソッドを使用すると、一行ずつ読み込むことができます。
今回はPeekメソッドで末尾を確認していますが、ReadLineメソッドで最後までファイルを読み込むとnullが返ってくるので、nullで末尾の確認をすることも可能です。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | using System; using System.IO; using System.Text; class FileRead {   static void Main() {     StreamReader streamReader = new StreamReader(         "readme.txt", Encoding.GetEncoding("Shift_JIS"));     while (streamReader.Peek() > -1)     {         Console.WriteLine(streamReader.ReadLine());     }     streamReader.Close();   } } | 
③一文字ずつ読み込む
Readメソッドを使用すると、一文字ずつ読み込むことができます。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | using System; using System.IO; using System.Text; class FileRead {   static void Main() {     StreamReader streamReader = new StreamReader(         "readme.txt", Encoding.GetEncoding("Shift_JIS"));     while (streamReader.Peek() > -1)     {         Console.Write(Convert.ToChar(streamReader.Read()));     }     streamReader.Close();   } } | 
④指定した文字数ずつ読み込む
ReadBlockメソッドを使用すると、指定した文字数を読み込むことができます。
下記は、10文字ずつ読み込む例になります。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | using System; using System.IO; using System.Text; class FileRead {   static void Main() {     StreamReader streamReader = new StreamReader(         "readme.txt", Encoding.GetEncoding("Shift_JIS"));     while (streamReader.Peek() > -1)     {         char[] c = new char[10];         streamReader.ReadBlock(c, 0, c.Length);         Console.Write(c);     }     streamReader.Close();   } } | 
ファイルを閉じる
StreamReaderクラスを使用した際は、使用後にCloseメソッドを使用して閉じる必要が発生します。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using System; using System.IO; using System.Text; class FileRead {   static void Main() {     StreamReader streamReader = new StreamReader(         "readme.txt", Encoding.GetEncoding("Shift_JIS"));     string str = streamReader.ReadToEnd();     streamReader.Close();     Console.Write(str);   } } | 
ただし、Closeメソッドを使用する場合以外にusingステートメントによるファイルのクローズをすることも可能です。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using System; using System.IO; using System.Text; class FileRead {   static void Main() {     string str = "";     using (StreamReader streamReader = new StreamReader(         "readme.txt", Encoding.GetEncoding("Shift_JIS"))) {       str = streamReader.ReadToEnd();     }     Console.Write(str);   } } | 
usingステートメントの場合は、実行終了時StreamReaderクラスのDisposeメソッドが呼び出され、その中でCloseメソッドが自動的に呼び出されます。
【エンジニア募集中】フルリモートも◎(リモート率85.7%)、平均残業8時間、年休124日以上、有給取得率90% etc. 詳細はこちらから>

 
                                     
                                     
                                     
                                    

