So I have a function call classifier that basically check all text file in a directory, if there is any text file contains more than 50 line of words, then classify that text file into a type using other class called Text_Classification which I am sure its correct and error free. After classification, I will need to clean that text file and write a “Lines” as a first new line on that text file (this is for other class, so dont bother 🙂 ).
But I got an exception, which mean there is something wrong in the try{} block.
Any idea why?
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");
}
}
}
}
As discussed, this line is the culprit:
The class
Text_Classificationis opening and not closing the filetextfile_name.