程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 讀寫文本文件 ---字符行式讀取

讀寫文本文件 ---字符行式讀取

編輯:C++入門知識




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

------------------------------------------------------------------------------------------------------------





  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved