Read/write text files-character row-based reading

Source: Internet
Author: User



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 ());}}}------------------------------------------------------------------------------------------------------------


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.