2017-01-03 47 views
0

我寫下面的代碼來重新格式化源文本文件(columns_original)並寫入輸出文件(output.ctl)。它正在工作,但我應該做的是,在由以下代碼創建的輸出文件的最後一行中,文件末尾有一個「,」,我想將其更改爲「。」。我應該在哪個部分嵌入此代碼?我說這個在最後,但我得到一個錯誤「類型‘System.IO.IOException’未處理的異常出現在mscorlib.dll輸出文件正被另一個程序使用」如何用「」替換「,」。「在文件的最後一行?

// I am trying to replace "," with "." at the last line of the text I created  above and I created below code. 

// I am trying to replace "," with ")" at the last line of the text I created above and 
using (FileStream aFile2 = new FileStream(path, FileMode.Append, FileAccess.Write)) 
using (StreamWriter sw2 = new StreamWriter(aFile2)) 
{ 
    var lastLine = File.ReadLines(path).Last(); 
    lastLine = lastLine.Replace(",", "."); 
    sw2.WriteLine(lastLine); 
} 

MCVE:

// I ADDED ABOVE CODE TO BELOW WORKING PART AND I AM GETTING ERROR MENTIONED IN THE POST 


using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions; 
using System.Threading.Tasks; 

namespace CTL_CMD_Creator 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     string ColChangable1 = "VARCHAR2"; 
     string ColChangable2 = "CHAR"; 
     string ColChangable3 = "NUMBER"; 
     string ColChangable4 = "DATE"; 

     string path = @"C:\Users\***\Desktop\output.ctl"; 

     StreamReader reader = File.OpenText(@"C:\Users\***\Desktop\Columns_Original.txt"); 
     string line; 

     using (FileStream aFile = new FileStream(path, FileMode.Append, FileAccess.Write)) 
     { 
      using (StreamWriter sw = new StreamWriter(aFile)) 
      { 
       while ((line = reader.ReadLine()) != null) 
       {      

        string[] tokens = line.Split(new char[] { ' ', '\t', '(', ')', ',' }, StringSplitOptions.RemoveEmptyEntries); 


        if (tokens[1].Equals(ColChangable1) || tokens[1].Equals(ColChangable2)) 
        { 
         sw.WriteLine(tokens[0] + "\t" + "\"TRIM(:" + tokens[0] + ")\","); 
        } 

        else if (tokens[1].Equals(ColChangable3)) 
        { 
         sw.WriteLine(tokens[0] + "\t" + ","); 
        } 
        else if (tokens[1].Equals(ColChangable4)) 
        { 
         sw.WriteLine(tokens[0] + "\t" + "DATE" + ","); 
        }       
       } 
      }     
     } 
    } 
} 
} 
+0

你能張貼在dotnetfiddle? – aloisdg

+0

很難理解你在哪裏調用替換函數以及失敗的地方。 –

+0

您可能會重新考慮您的主循環。例如,您可以只執行一個簡單的寫操作來寫入除「,」之外的行,然後在下一次迭代時僅執行簡單的WriteLine(「,」),並且在退出循環之後 - 執行額外的WriteLine(「。」) –

回答

1

如果我理解你的問題,更簡單的方法是使用System.IO.File。 Demo on .NetFiddle

using System; 
using System.IO; 
using System.Linq; 

public class Program 
{ 
    public static void Main(string[] args) 
    { 
     // init 
     var path = "output.ctl"; 
     var content = 
      "Hello and Welcome" + Environment.NewLine 
      + "Hello and Welcome" + Environment.NewLine 
      + ","; 
     File.WriteAllText(path, content); 

     var text = File.ReadAllLines(path); // read the file as string[] 

     foreach (var line in text) // print the file 
      Console.WriteLine(line); 

     text[text.Length - 1] = text.Last().Replace(",", "."); // replace 

     File.WriteAllLines(path, text); // overwrite or write to a new file 

     string[] lines2 = File.ReadAllLines(path); // read again 
     foreach (var line in lines2) // then print to show the difference 
      Console.WriteLine(line); 
    } 
} 

輸出:

Hello and Welcome 
Hello and Welcome 
, 
Hello and Welcome 
Hello and Welcome 
. 
+0

你得到整個文件到緩衝區。如果文件太大,則會導致內存不足。 –

+0

@MaxMokrousov真。 – aloisdg

+0

這個答案有竅門。非常感謝您的關注。 – Lyrk

3

使用reader.EndOfStream來檢查它是否爲文件結尾。

 while ((line = reader.ReadLine()) != null) 
     { 

      string[] tokens = line.Split(new char[] { ' ', '\t', '(', ')', ',' }, StringSplitOptions.RemoveEmptyEntries); 
      string line; 

      if (tokens[1].Equals(ColChangable1) || tokens[1].Equals(ColChangable2)) 
      { 
       line = tokens[0] + "\t" + "\"TRIM(:" + tokens[0] + ")"; 
      } 

      else if (tokens[1].Equals(ColChangable3)) 
      { 
       line = tokens[0] + "\t"; 
      } 
      else if (tokens[1].Equals(ColChangable4)) 
      { 
       line = tokens[0] + "\t" + "DATE"; 
      } 

      line += reader.EndOfStream ? "." : ","; 

      sw.WriteLine(line); 
     } 
+0

這附加結束,所以我得到了兩次最後一行。感謝您的好意回覆我的帖子。 – Lyrk

2

請問下面的代碼對你有幫助嗎?

我覺得使用(.hasNext()) 來測試最終寫入會更容易,而不是關閉和打開新的文件流。

這是你的問題,你試圖打開一個文件時寫入訪問,當它仍然是開放的。

using (FileStream aFile = new FileStream(path, FileMode.Append, FileAccess.Write)) 
    { 
     using (StreamWriter sw = new StreamWriter(aFile)) 
     { 
      while ((line = reader.ReadLine()) != null) 
      {      
       var delimiter = string.Empty; 

       string[] tokens = line.Split(new char[] { ' ', '\t', '(', ')', ',' }, StringSplitOptions.RemoveEmptyEntries); 

       if(tokens.hasNext()) 
       { delimiter = ","; } 
       else 
       { delimiter = "."; } 

       if (tokens[1].Equals(ColChangable1) || tokens[1].Equals(ColChangable2)) 
       { 
        sw.WriteLine(tokens[0] + "\t" + "\"TRIM(:" + tokens[0] + ")\" + delimiter); 
       } 

       else if (tokens[1].Equals(ColChangable3)) 
       { 
        sw.WriteLine(tokens[0] + "\t" + delimiter); 
       } 
       else if (tokens[1].Equals(ColChangable4)) 
       { 
        sw.WriteLine(tokens[0] + "\t" + "DATE" + delimiter); 
       }       
      } 
     }     
    } 
} 
+0

謝謝你的回答和你的時間。 – Lyrk

0

對於您可以使用較小的文件:

public void Replace(string inputFile, string outputFile) 
    { 
     File.WriteAllText(outputFile, Regex.Replace(File.ReadAllText(inputFile), @",\s*$", ".")); 
    } 
+0

非常感謝。這個答案很整潔。 – Lyrk