2013-03-22 1 views
0

내 용도는 따옴표 붙일 데이터가 들어있는 파일의 위치를 ​​탐색하여 텍스트 파일에서 데이터를 읽는 것입니다. 파일의 데이터는 목록에 저장됩니다.하나의 열에있는 텍스트 파일에 데이터를 쓰는 중 논리적 오류가 발생했습니다.

'20050000',  
'40223120',  
'40006523'  
But my code is currently displaying the output in the format: 
'20050000'20050000, 
'40223120'20050000, 
'40006523'40006523  

Pls는 도움말 : 내가

Example of a string:  
20050000  
40223120  
40006523 

샘플을 넣어 아래 demostrated으로 다음 단일 컬럼에 데이터를 저장하는 출력 파일을 만들 ArrayList를 통해 데이터 및 루프를 얻을 수 및 각 문자열을 연결하는 ArrayList를를 사용 . 당신이 나쁜 루프가 button2_Click 당신의 방법에서

public List<string> list() 
    { 
     List<string> Get_Receiptlist = new List<string>(); 
     String ReceiptNo; 
     openFileDialog1.ShowDialog(); 
     string name_of_Textfile = openFileDialog1.FileName; 
     try 
     { 
      StreamReader sr = new StreamReader(name_of_Textfile); 
      { 
       while ((ReceiptNo = sr.ReadLine()) != null) 
       { 
        Get_Receiptlist.Add(ReceiptNo); 
       } // end while 
       MessageBox.Show("Record saved in the Data list");// just for testing purpose. 
      }// end StreamReader 

     } 
     catch (Exception err) 
     { 
      MessageBox.Show("Cannot read data from file"); 
     } 
     return Get_Receiptlist; 
    } 
    private void button2_Click(object sender, EventArgs e) 
    { 
     string single_quotation = "'"; 
     string comma = ","; 
     string paths = @"C:\Users\sample\Desktop\FileStream\Output.txt"; 
     if (!File.Exists(paths)) 
     { 
      // Create a file to write to. 
      using (StreamWriter sw = File.CreateText(paths)) 
      { 
       string[] receipt = list().ToArray(); 
       foreach (string rec in receipt) 
       { 
        string quoted_receipt = single_quotation + rec + single_quotation + rec + comma; 
        sw.WriteLine(quoted_receipt); 
        sw.WriteLine(Environment.NewLine); 
       }//foreach 

       sw.Close(); 

       MessageBox.Show("Finish processing File"); 
      }//end using 
     }// end if 
    } 
+0

코드의 관련 부분을 제공해야합니다. – user1697575

+0

방금 ​​코드를 게시했습니다. 나는 밖으로했다 – kombo

답변

1

:

string[] receipt = list().ToArray(); 
foreach (string rec in receipt) 
{ 
    string quoted_receipt = single_quotation + rec + single_quotation + rec + comma; 
    sw.WriteLine(quoted_receipt); 
    sw.WriteLine(Environment.NewLine); 
}//foreach 

먼저 나는 자바 모르겠어요 ...하지만 자바 있다면, 나는이이 조각을 대체 할 것이다 :

List<String> values = list(); 
for (int i = 0; i < values.size(); i++) 
{ 
    String rec = values.get(i); 
    StringBuilder quoted_receipt = new StringBuilder(); 
    if (i > 0) 
    { 
    // add comma only if the first iteration already passed 
    quoted_receipt.append(comma); 
    } 
    quoted_receipt.append(single_quotation).append(rec).append(single_quotation); 
    sw.WriteLine(quoted_receipt.toString()); 
    sw.WriteLine(Environment.NewLine); 
} 
+0

@ user1697575.Am C#을 사용하여 감사하지만 귀하의 대답은 정확했다. – kombo

+0

문제 없습니다. "C# : tag"를 질문에 넣었 으면 좋겠지 만 관심 분야를보다 정확히 파악할 수 있습니다. – user1697575

관련 문제