2017-03-11 4 views
1

テキストファイルを1行ずつ読み込み、複数の行から1行を作成しようとしています。最後はnです。私のデータは次のようになります。私は試してみましたStreamReader.Readline()の最後にenvironment.newlineがあるかどうかを確認するには

FileStream fsFileStream = new FileStream(strInputFileName, FileMode.Open, 
FileAccess.Read, FileShare.ReadWrite); 

using (StreamReader srStreamRdr = new StreamReader(fsFileStream)) 
{ 
    while ((strDataLine = srStreamRdr.ReadLine()) != null && !blnEndOfFile) 
    { 
     //code evaluation here 
    } 

BusID|Comment1|Text\r\n 
1010|"Cuautla, Inc. d/b/a 3 Margaritas VIII State Lic. #40428210000 City Lic.#4042821P 9/26/14  9/14/14 - 9/13/15 $175.00 9/20/00 9/14/00 - 9/13/01 $575.00 New License"\r\n 
1020|"7-Eleven Inc., dba 7-Eleven Store #20638 State Lic. #24111110126; City Lic. #2411111126P SEND ISSUED LICENSES TO DALLAS, TX\r\n 

私のコードは次のようである:

BusID|Comment1|Text\r\n 
1010|"Cuautla, Inc. d/b/a 3 Margaritas VIII\n 
State Lic. #40428210000 City Lic.#4042821P\n 
9/26/14  9/14/14 - 9/13/15 $175.00\n 
9/20/00 9/14/00 - 9/13/01 $575.00 New License"\r\n 
1020|"7-Eleven Inc., dba 7-Eleven Store #20638\n 
State Lic. #24111110126; City Lic. #2411111126P\n 
SEND ISSUED LICENSES TO DALLAS, TX\r\n 

私は、データが次のようになりたい

if (strDataLine.EndsWith(Environment.NewLine)) 
{ 
    blnEndOfLine = true; 
} 

および

if (strDataLine.Contains(Environment.NewLine)) 
{ 
    blnEndOfLine = true; 
} 

これらは、文字列変数の最後には何も表示されません。これらの行を1つの行にまとめることができるように、本当の行末を伝える方法はありますか?私はファイルを別に読んでいるべきですか?

+0

あなたは\ rをする\ nは離れテキストから取り除か – Steve

+0

このファイルがどのくらいあるが返されReadLineメソッドを使用している場合は?それをすべてメモリにロードする余裕はありますか? – Steve

答えて

0

あらゆる種類の改行があるため、StringReaderのReadLineメソッドを使用することはできません。 \r\nとの両方が入力から削除され、その行がリーダーによって返され、削除された文字が\ r \ nか\ nかどうかはわかりません。

ファイルが本当に大きくない場合はあなたのファイルは、あなたがメモリ内のすべてを読み込むことはできませんが、あなたがする必要があります(あなたは3.5ギガバイトを言うように)本当に大きい場合は、メモリ内のすべてをロードし、別の行に自分自身を分割

// Load everything in memory 
string fileData = File.ReadAllText(@"D:\temp\myData.txt"); 

// Split on the \r\n (I don't use Environment.NewLine because it 
// respects the OS conventions and this could be wrong in this context 
string[] lines = fileData.Split(new string[] { "\r\n"}, StringSplitOptions.RemoveEmptyEntries); 

// Now replace the remaining \n with a space 
lines = lines.Select(x => x.Replace("\n", " ")).ToArray(); 

foreach(string s in lines) 
    Console.WriteLine(s); 

EDIT
をしようブロック単位で処理します。幸いなのStreamReaderは、私たちはこのコードは、あなたのファイルは常にの\ r \ nで終わることを前提とし、この

// Where we store the lines loaded from file 
List<string> lines = new List<string>(); 

// Read a block of 10MB 
char[] buffer = new char[1024 * 1024 * 10]; 
bool lastBlock = false; 
string leftOver = string.Empty; 

// Start the streamreader 
using (StreamReader reader = new StreamReader(@"D:\temp\localtext.txt")) 
{ 
    // We exit when the last block is reached 
    while (!lastBlock) 
    { 
     // Read 10MB 
     int loaded = reader.ReadBlock(buffer, 0, buffer.Length); 

     // Exit if we have no more blocks to read (EOF) 
     if(loaded == 0) break; 

     // if we get less bytes than the block size then 
     // we are on the last block 
     lastBlock = (loaded != buffer.Length); 

     // Create the string from the buffer 
     string temp = new string(buffer, 0, loaded); 

     // prepare the working string adding the remainder from the 
     // previous loop 
     string current = leftOver + temp; 

     // Search the last \r\n 
     int lastNewLinePos = temp.LastIndexOf("\r\n"); 

     if (lastNewLinePos > -1) 
     { 
      // Prepare the working string 
      current = leftOver + temp.Substring(0, lastNewLinePos + 2); 

      // Save the incomplete parts for the next loop 
      leftOver = temp.Substring(lastNewLinePos + 2); 
     } 
     // Process the lines 
     AddLines(current, lines); 
    } 
} 

void AddLines(string current, List<string> lines) 
{ 
    var splitted = current.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); 
    lines.AddRange(splitted.Select(x => x.Replace("\n", " ")).ToList()); 
} 

のようなコードを実装するために、あなたは常にブロック内の\ r \ nを得ることができますReadBlockと呼ばれる方法を提供し、 10MBのテキストです。実際のデータでは、さらに多くのテストが必要です。

+0

これは私が現在使用しているファイルに最適です!ありがとうございました。ファイルサイズの制限について知っていますか?我々は、3.5ギガのようないくつかのかなり大きなファイルを持つことができます。大きなファイルでこれを行う方法に関するアイデアはありますか? – Cass

+0

File.ReadAllTextで読み込むには大きすぎます。この時点で、そのファイルのチャンクをメモリにロードし、上で説明したように行を処理し、次のチャンクに対して再起動する特殊なコードが必要です。 – Steve

+0

理想的なサイズのためには、どれくらいのメモリを使用するかによって多くのことが決まります。私は時間で100MBのブロックにとどまります – Steve

0

あなたが投稿したものがファイル内のwhatsである場合。確かに書かれている\ r \ nを意味、あなたがそれらをエスケープ解除するには、次のように使用することができます。

strDataLine.Replace("\\r", "\r").Replace("\\n", "\n"); 

これはあなたが今のように、あなたの比較を行うためにEnvironment.NewLineを使用できることを確認します。

if (strDataLine.Replace("\\r", "\r").Replace("\\n", "\n").EndsWith(Environment.NewLine)) 
{ 
    blnEndOfLine = true; 
} 
0

あなただけFile.ReadAllText(path)を呼び出すことにより、すべてのテキストを読み、次のようにそれを解析することができます

  string input = File.ReadAllText(your_file_path); 
      string output = string.Empty; 
      input.Split(new[] { Environment.NewLine } , StringSplitOptions.RemoveEmptyEntries). 
       Skip(1).ToList(). 
       ForEach(x => 
       { 
        output += x.EndsWith("\\r\\n") ? x + Environment.NewLine 
                : x.Replace("\\n"," "); 
       }); 
関連する問題