2014-03-03 165 views
1

我有以下行的文本文件(比如text1.txt):如何讀取行中特定單詞的文本文件並將整行寫入另一個文本文件?

4410 Rel testRel1 
4411 Dbg testDbg1 
4412 Dbg testDbg2 
4412 Rel testRel2 
4413 Rel testRel3 
4413 Dbg testDbg3 
4414 Rel testRel4 
4415 Rel testRel5 

現在,我想寫所有以「相對」兩個字到一個文本文件(比如text2.txt)線。所以我text2.txt應該是這樣的:

4410 Rel testRel1 
4412 Rel testRel2 
4413 Rel testRel3 
4414 Rel testRel4 
4415 Rel testRel5 

Atlast,我的代碼應該閱讀text2.txt的前四個字符返回text2.txt採取text1.txt的路徑作爲輸入的最後一行(即4415)的。 以下是我的代碼。可能是我可能寫了一半,不知道c#。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Activities; 
using Microsoft.Build.Utilities; 
using Microsoft.Build.Framework; 
using System.IO; 
using Microsoft.TeamFoundation.Build.Client; 

namespace Build_Tasks.Activities 
{ 
    [BuildActivity(HostEnvironmentOption.All)] 
    public sealed class GetBuildNumber : CodeActivity 
    { 
     // Define an activity input argument of type string 
     public InArgument<string> TextFileName { get; set; } 
     public OutArgument<string> setBuildNumber { get; set; } 
     private string temp_FilePath = "c:\\temp.txt"; 

     // If your activity returns a value, derive from CodeActivity<TResult> 
     // and return the value from the Execute method. 
     protected override void Execute(CodeActivityContext context) 
     { 
      // Obtain the runtime value of the Text input argument 
      string TextFileName = context.GetValue(this.TextFileName); 
      string TextFilePath = TextFileName; 
      string[] Lines = File.ReadAllLines(TextFilePath); 
      //string wordrel = "Rel"; 
      System.IO.StreamReader file = new System.IO.StreamReader(TextFilePath); 
      List<string> Spec = new List<string>(); 
      string line; 
      System.IO.StreamWriter file2 = new System.IO.StreamWriter(temp_FilePath); 
      while ((line = file.ReadLine()) != null) 
      { 
       if(line.Contains("Rel")) 
        { 
         file2.WriteLine(line); 
         } 
      } 
      var lastline = File.ReadLines(temp_FilePath).Last(); 
      string number = lastline.Substring(0, 4); 
      context.SetValue<string>(this.setBuildNumber, number); 
      } 
    } 
    } 
+0

如果有任何其他裝置等讀取該行的陣列,而不是text2.txt,甚至沒有什麼幫助。我的text2.txt是一個臨時文件,用於實現上面指定的。 – Bala

+0

您不必創建'file2.txt',也不需要將行存儲在數組中,只是爲了獲取內部編號,正如我在答案中指出的那樣。 – twoflower

回答

2

如果你需要file2爲是要得到內部版本號,您根本不必創建它:

protected override void Execute(CodeActivityContext context) 
{ 
    // Obtain the runtime value of the Text input argument 
    string TextFileName = context.GetValue(this.TextFileName); 
    string TextFilePath = TextFileName; 

    string number = null; 
    var splitChars = new[]{ ' ' }; 

    foreach (var line in File.ReadLines(TextFilePath)) 
    { 
     var values = line.Split(splitChars, StringSplitOptions.RemoveEmptyEntries).ToArray(); 
     if (values.Length < 3) 
      continue; 

     buildNumber = (values[1] == "Rel" ? values[0] : buildNumber); 
    } 

    context.SetValue<string>(this.setBuildNumber, number);   
} 

而且因爲你只在最後版本號感興趣,這還可以通過不讀從一開始的文件,但求流結束,跳回到直到你找到Rel行了優化。

+0

你的意思是優化部分? – twoflower

+0

我得到這個錯誤:索引超出了數組的範圍 – Bala

+0

是的,需要幫助的優化部分以及 – Bala

0

這將遍歷原始文件的線和相關的行復制到新的文件:

string tempFile = "text2.txt"; 
List<string> linesWithREL = new List<string>(); 
using (var sr = new StreamReader("file.txt")) 
using (var sw = new StreamWriter(tempFile)) 
{ 
    string line; 

    while ((line = sr.ReadLine()) != null) 
    { 
     //check if the current line should be copied 
     if (line.Contains("whatever")) 
     { 
       linesWithREL.Add(line.Substring(0,4)); 
       sw.WriteLine(line); 
     } 
    } 
} 
0

試試這個:

string path1 = @"C:\data1.txt"; 
string path2 = @"C:\data2.txt"; 
string searchKey = "Rel"; 
List<string> newlines = new List<string>(); 
foreach (var line in File.ReadLines(path1)) 
{ 
if (line.Split(new char[]{' '}, 
     StringSplitOptions.RemoveEmptyEntries)[1].Contains(searchKey)) 
{ 
newlines.Add(line); 
} 
} 
File.WriteAllLines(path2,newlines.ToArray()); 
+0

我可以將行寫入另一個文件。但是我的第二個問題是我面臨的問題(閱讀最後一行的前四個字符) – Bala

+0

@Bala您必須先使用不同的方法訪問相同的文件之前先關閉閱讀器。 – sasjaq

+0

@Bala作家,當然,不是讀者:) – sasjaq

0

好了,因爲我看了一些評論,你的問題是不是在讀/寫的,但在這一行:

var lastline = File.ReadLines(temp_FilePath).Last(); 

你必須先關閉您的作家,在同一文件中使用File.ReadLine前。

file2.Flush(); 
file2.Close(); 
var lastline = File.ReadLines(temp_FilePath).Last(); 

如果你想優化你的代碼,添加了幾個答案,不需要重複。

3

嘗試此....

static void Main(string[] args) 
    { 
     int counter = 0; 
     string line; 

     // Read the file(your SXA file) and display it line by line. 
     System.IO.StreamReader file = new System.IO.StreamReader("c:\\SXA63.txt"); 
     while((line = file.ReadLine()) != null) 
     {   //File to write lines which contain Rel. 
        using(StreamWriter writer = new StreamWriter("c:\\Relfile.txt",true)) 
        { 
         if(line.Contains("Rel")) 
         writer.WriteLine(line); 
        } 
      counter++; 

     } 
     String last = File.ReadLines(@"C:\Relfile.txt").Last(); 
     string buildNo = last.Substring(0, 4); 
     file.Close(); 
     Console.ReadKey(); 

    } 
} 
相關問題