首页 > 代码库 > 读写文本文件 ---字符行式读取
读写文本文件 ---字符行式读取
File 类 File.OpenWrite 方法 StringWriter 类 File.open //using (StreamWriter sw2 = File.CreateText(cmdFile)) using (StreamWriter sw2 = new StreamWriter(cmdFile,false, Encoding.Default)) //指定写入的编码格式 //-------------------------------------------------------------------------------- string logDirectory = Directory.GetCurrentDirectory()+ @"\LOG"; if (!System.IO.Directory.Exists(logDirectory)) { Directory.CreateDirectory(logDirectory);//路径不存在创建 } //-------------------------------------------------------------------------------- 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 statement 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(' ');//将输入的字符串根据指定标点符号分割 // 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( //---------------------------------------------------------------------------------------------------------------- Module 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); //创建一个新文件,在其中写入指定的字符串数组,然后关闭该文件。如果目标文件已存在,则覆盖该文件 //---------------------------------------------------------------------------------------------------------------- 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 to. 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()); } } } ------------------------------------------------------------------------------------------------------------
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。