2012-09-11 2 views
0

폴더 당 긴 텍스트 파일 목록 (약 80-100 개 파일)이 있습니다. 나는 매 4 개의 텍스트 파일을 하나의 텍스트 파일로 결합해야한다. 각 텍스트 파일은 별도의 SQL 삽입 명령이므로 첫 번째 4 개의 텍스트 파일의 첫 번째 줄을 출력 파일에 보관해야합니다 (INSERT INTO 데이터베이스 (value1, value2, value3) VALUES). 나머지 3 개의 파일은 건너 뜁니다 첫 번째 줄에는 값이 있습니다.많은 텍스트 파일을 4 개의 그룹으로 결합하는 방법

내가 겪고있는 문제는 4 개의 파일 중 마지막 파일을 결합 할 때 마지막 파일의 절반 만 새 결합 파일에 복사한다는 것입니다.

한 파일에 3 개의 파일 또는 2 개의 파일 만 결합하려고해도. 마지막 파일의 약 절반 만 복사합니다.

여기 내 코드입니다.

fourCount은 네 번째 텍스트 파일이 결합 된시기를 나타냅니다.

 string[] array2 = sqlInsertList.ToArray(); 
     StreamWriter outfile3 = new StreamWriter(folderPath.Text + "\\" + count + ".txt"); 
     count++; 

     foreach (string dirFileName in array2) 
     { 
      StreamReader readFile = new StreamReader(dirFileName); 
      string readFromFile = readFile.ReadLine(); 

      if(fourCount == 1) 
      { 
       outfile3 = new StreamWriter(folderPath.Text + "\\" + count + ".txt"); 
       outfile3.WriteLine(readFromFile); 
      } 


      while(!readFile.EndOfStream) 
      { 
       readFromFile = readFile.ReadLine(); 
       outfile3.WriteLine(readFromFile); 
      } 

      count++; 
      if(fourCount == 4) 
      { 
       outfile3.WriteLine(";"); 
       fourCount = 1; 
      } 
      else 
      { 
       fourCount++; 
      } 
     } 

답변

3

출력 파일을 플러시하지 않았기 때문에 닫히지 않았습니다.

변경 그것에 :

using(StreamWriter outfile3 = new StreamWriter(...)) 
{ 
    count++; 

    foreach (string dirFileName in array2) 
    { 

     if(fourCount == 1) 
     { 
      outfile3.Close(); // add this 
      outfile3 = new StreamWriter(folderPath.Text + "\\" + count + ".txt"); 
      outfile3.WriteLine(readFromFile); 
     } 

     ... 
    } 
} 
+0

나는 그것을 닫은 후에 잘 작동했다! 감사! – user908759

0

당신이 진정한 속성에 자동 세척을 설정하거나 세척 방법,
이 스트림을 떠나 좋은 그렇게 열어서는 것 그러나 사용할 수 있습니다 가능하면 때를 닫습니다 당신이 그것을 필요로하지 않을 것이라는 점을 알고있다

관련 문제