zoukankan      html  css  js  c++  java
  • 读写文本文件 ---字符行式读取



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


  • 相关阅读:
    Redis学习笔记
    sysctl命令修改内核参数
    Spring Boot学习笔记
    抽象和接口
    FTP主动模式与被动模式
    JVM参数调优
    JVM性能调优工具
    性能调优工具
    Feign Hystrix Tomcat参数配置
    Zuul学习笔记
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/3813309.html
Copyright © 2011-2022 走看看