2012-05-15 5 views
0

그래서 나는 기본적으로 디렉토리의 모든 텍스트 파일을 확인하는 함수 호출 분류기가 있습니다. 텍스트 파일에 50 줄 이상의 단어가 포함되어 있으면 해당 텍스트 파일을 사용하는 유형으로 분류합니다 Text_Classification이라는 다른 클래스는 정확하고 오류가 없다고 확신합니다. 분류가 끝나면 해당 텍스트 파일을 지우고 그 텍스트 파일의 첫 줄에 "Lines"를 써야합니다 (이것은 다른 클래스 용입니다. 그러나 예외가 발생했습니다. 즉, try{} 블록에 잘못된 것이 있습니다.텍스트 파일을 다시 쓰는 동안 알 수없는 예외 가져 오기

왜 그런가? 논의 된 바와 같이

static void classifer(object state) { 
    Console.WriteLine("Time to check if any log need to be classified"); 
    string[] filename = Directory.GetFiles(@"C:\Users\Visual Studio 2010\Projects\server_test\log"); 
    foreach(string textfile_name in filename) { 
     var lineCount = File.ReadAllLines(textfile_name).Length; 
     if(lineCount > 50) { 
      Console.WriteLine("Start classifying 1 of the logs"); 
      Text_Classification classifier = new Text_Classification(textfile_name, 1); 
      string type = classifier.passBack_type(); //this function passes back the type of the log(text file) 
      Console.WriteLine(type); 
      try { 
       TextWriter tw = new StreamWriter(textfile_name); //clean the text file 
       tw.WriteLine(" "); 
       tw.Close(); 
       TextWriter tw2 = new StreamWriter(textfile_name, true); //append <Lines> as the first new line on the text file 
       tw2.WriteLine("Lines"); 
       tw2.Close(); 
      } 
      catch { 
       Console.WriteLine("cant re-write txt"); 
      } 
     } 
    } 
} 
+3

 File.WriteAllLines(textfile_name, new String[]{" "}); File.AppendAllLines(textfile_name, new String[]{" Lines "}); 

당신이 시도/캐치하지 않고이 코드를 다시 실행하십시오 수 다음 사용해 볼 수 있습니까? 적어도 catch 블록에서 오류를 포착하고 기록하여 오류가 무엇인지 알 수 있습니다. 'catch (Exception e) {Console.WriteLine (e.ToString()); }' – caesay

+0

그래, 내 잘못, 지금은 그것을 실행하는 오류를 캡처해야합니다 ... –

+0

그것은 텍스트 파일이 다른 프로세스에 의해 사용되고 있다고 말했고, 나는 함수 호출로 코드를 실행하려고 시도했는데 성공적으로 실행되었습니다 ... –

답변

1

,이 선이 원인이다

Text_Classification classifier = new Text_Classification(textfile_name, 1); 

Text_Classification을 열고 파일 textfile_name 폐쇄되지 클래스.

0

당신은 디버거가 예외를 잡을 수 있도록 대신

   try 
       { 
        TextWriter tw = new StreamWriter(textfile_name); //clean the text file 
        tw.WriteLine(" "); 
        tw.Close(); 


        TextWriter tw2 = new StreamWriter(textfile_name, true); //append <Lines> as the first new line on the text file 
        tw2.WriteLine("Lines"); 
        tw2.Close(); 
       } 
       catch { 
        Console.WriteLine("cant re-write txt"); 
       } 
관련 문제