2010-04-29 4 views
2

텍스트 파일을 한 줄씩 다른 텍스트 파일로 복사하려고합니다. 1024 자의 버퍼가있는 것 같습니다. 내 파일에 1024 자 미만인 경우, 내 기능은 다른 파일에서 복사되지 않습니다.텍스트 파일 복사

또한 1,024 문자보다 작지 만 1024보다 작은 문자는 이러한 초과 문자가 복사되지 않습니다.

예 : 초기 파일에

2048 문자 - 2048 초기 파일에

988 문자를 복사 - 0

1256 문자 초기 파일에

을 복사 - 1024

private void button3_Click(object sender, EventArgs e) 
{ 
    // écrire code pour reprendre le nom du fichier sélectionné et 
    //ajouter un suffix "_poly.txt" 
    string ma_ligne; 
    const int RMV_CARCT = 9; 

    //délcaration des fichier 
    FileStream apt_file = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read); 
    textBox1.Text = textBox1.Text.Replace(".txt", "_mod.txt"); 
    FileStream mdi_file = new FileStream(textBox1.Text, FileMode.OpenOrCreate,FileAccess.ReadWrite); 

    //lecture/ecriture des fichiers en question 
    StreamReader apt = new StreamReader(apt_file); 
    StreamWriter mdi_line = new StreamWriter(mdi_file, System.Text.Encoding.UTF8, 16); 



    while (apt.Peek() >= 0) 
    { 
     ma_ligne = apt.ReadLine(); 
     //if (ma_ligne.StartsWith("GOTO")) 
     //{ 
     // ma_ligne = ma_ligne.Remove(0, RMV_CARCT); 
     // ma_ligne = ma_ligne.Replace(" ",""); 
     // ma_ligne = ma_ligne.Replace(",", " "); 
     mdi_line.WriteLine(ma_ligne); 
     //} 
    } 
    apt_file.Close(); 
    mdi_file.Close(); 
} 
+0

'button3_Click (...)'... oof. ;) –

답변

6

두 가지 문제 :

  1. 귀하의 FileStream, StreamWriterStreamReader 클래스는 Peek()없이 구현 제안 안에 있어야합니다 using { } 블록. 그들은 IDisposable을 구현하므로 Dispose을 호출해야하며 using 블록이이를 처리합니다. 당신이 이것을한다면, 그것은 실제로 당신이 고쳐야 만하는 전부입니다. 이렇게하면 더 이상 Close() 번으로 전화 할 필요가 없습니다.
  2. 최소한 닫기 전에 mdi_line.Flush()으로 전화하십시오. 그러면 버퍼가 즉시 파일에 기록됩니다. StreamWriter 클래스 인 타이틀에 Dispose를 호출

using 블록이 문제를 해결합니다 이유입니다, Flush를 호출합니다.

using (FileStream apt_file = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read)) 
{ 
    textBox1.Text = textBox1.Text.Replace(".txt", "_mod.txt"); 

    using (FileStream mdi_file = new FileStream(textBox1.Text, FileMode.OpenOrCreate, FileAccess.ReadWrite)) 
    { 
     //lecture/ecriture des fichiers en question 
     using (StreamReader apt = new StreamReader(apt_file)) 
     using (StreamWriter mdi_line = new StreamWriter(mdi_file, System.Text.Encoding.UTF8, 16)) 
     { 
      while (apt.Peek() >= 0) 
      { 
       ma_ligne = apt.ReadLine(); 
       //if (ma_ligne.StartsWith("GOTO")) 
       //{ 
       // ma_ligne = ma_ligne.Remove(0, RMV_CARCT); 
       // ma_ligne = ma_ligne.Replace(" ",""); 
       // ma_ligne = ma_ligne.Replace(",", " "); 
       mdi_line.WriteLine(ma_ligne); 
       //} 
      } 
     } 
    } 
} 
+0

mdi_lineFlush()는 훌륭하게 작동합니다! Thkx! – user90714

+2

@melt : 작동하지만, 필자가 묘사 한 것처럼'using' 블록을 사용해야합니다. 코드를 복사하여 응용 프로그램에 붙여 넣기 만하면됩니다. –

2

당신을 복사 그게 File.Copy()- 방법이 있다는 것을 알고 계십니까? 그리고 FileInfo has a Copy() - 방법도.
편집 : 복사가 진행되는 동안 파일 내용을 수정하고 싶습니다. 그러나 지금은 주석 처리되어 있습니다.

StreamReader의 사용 방법은 msdn documentation입니다.

using (StreamReader sr = new StreamReader("TestFile.txt")) 
{ 
     string line; 
     // Read and display lines from the file until the end of 
     // the file is reached. 
     while ((line = sr.ReadLine()) != null) 
     { 
      Console.WriteLine(line); 
     } 
} 
+0

복사 할 때 각 줄마다 처리하는 것처럼 보입니다.주석이 달린 코드에서 –

+0

이 나오면 파일을 한 줄씩 읽고 출력 파일에 기록하기 전에 각 줄을 수정하는 것이 목표 인 것 같습니다. – M4N

+0

@Shane, @Martin : 지적 해 주셔서 감사합니다. 당신이 옳을 수도 있고 내 대답이 그의 경우에 도움이되지 않을 수도 있습니다. – tanascius

0

는이 같은 ReadLine() method 사용하여 파일을 읽을 수 있습니다 :

using (StreamReader sr = new StreamReader(inputFile)) 
{ 
    String line; 
    while ((line = sr.ReadLine()) != null) 
    { 
     // process line 
    } 
} 
+0

이것은 그의 문제와 아무런 관련이 없습니다. –

0

어떻게 그냥 자신의 버퍼에 apt.ReadBlock()에 버퍼에 대해. 그런 다음 버퍼링 된 내용에서 \ r \ n을 (를) 분할하여 분할 배열의 행을 처리합니다. 분리 된 버퍼의 마지막 라인을 처리하지 말고, 다음 ReadBlock에 추가하고 헹굼을 반복하십시오.