File. openWrite method StringWriter class File. open // using (StreamWriter sw2 = File. createText (partition file) using (StreamWriter sw2 = new StreamWriter (partition file, false, Encoding. default) // specify the written encoding format // ---------------------------------------------------------------------------------- string logDirectory = Directory. getCurrentDirectory () + @ "\ LOG"; if (! System. IO. directory. exists (logDirectory) {Directory. createDirectory (logDirectory); // path creation does not exist} // empty private void openFile () {try {string fileName = getExcelFileName (); int counter = 0; int rows = 0; string line; int [] iArray = new int [16]; // Create an instance of StreamReader to read from a file. // The using statem Ent also closes the StreamReader. using (StreamReader sr = new StreamReader (fileName, System. text. encoding. getEncoding ("GB2312") {beans. con_open (); // Read and display lines from the file until the end of // the file is reached. while (line = sr. readLine ())! = Null) {counter ++; if (counter = 2) {string [] strNews = line. split (''); // splits the input string according to the specified punctuation. // iArray = new int [strNews. length]; for (int I = 0; I <strNews. length; I ++) {int iLen = strNews [I]. trim (). length; iArray [I] = iLen;} else {// try // {if (line. substring (0, 1 ). equals (// ---------------------------------------------------------------------------------------------------------------- Mod Ule module = new Module (); string dateStr = module. date_Format _ (DateTime. now. toShortDateString (); string timeStr = module. time_Format (DateTime. now. toString (); string txtPathName = SC. stockPath + fileName + dateStr + "_" + timeStr + ". txt "; if (revAlst = null) return; string [] arrayString = new string [revAlst. count]; revAlst. copyTo (arrayString); File. writeAllLines (txtPathName, arrayString); // create a new File, write the specified String Array in it, and then close the file. If the target file already exists, overwrite the file // using System; using System. IO; class Test {public static void Main () {string path = @ "c: \ temp \ MyTest.txt"; if (! File. exists (path) {// Create a file to write. using (StreamWriter sw = File. createText (path) {sw. writeLine ("Hello"); sw. writeLine ("And"); sw. writeLine ("Welcome") ;}// Open the file to read from. using (StreamReader sr = File. openText (fileName) {string s = ""; while (s = sr. readLine ())! = Null) {Console. writeLine (s) ;}try {string path2 = path + "temp"; // Ensure that the target does not exist. file. delete (path2); // Copy the file. file. copy (path, path2); Console. writeLine ("{0} was copied to {1 }. ", path, path2); // Delete the newly created file. file. delete (path2); Console. writeLine ("{0} was successfully deleted. ", path2);} catch (Exception e) {Console. writeLine ("The process failed: {0}", e. toString () ;}} else ());}}}------------------------------------------------------------------------------------------------------------