将某些行从一个文本文件复制到另一个文本

时间:2022-04-17 16:02:13

I need to open a file, read a line and if that line respect some conditions write it to another file, the line that I'm reading is a normal ASCII string representig HEX values and I need to paste it into a new file as HEX values and not as ASCII string.

我需要打开一个文件,读取一行,如果该行符合某些条件将其写入另一个文件,我正在阅读的行是一个普通的ASCII字符串代表HEX值,我需要将其粘贴到一个新文件中作为HEX值而不是ASCII字符串。

What I have is this:

我有的是这个:

private void Button_Click(object sender, RoutedEventArgs e)
{
    byte[] arrayByte = { 0x00 };
    var linesToKeep = File.ReadLines(fileName).Where(l => l.Contains(":10"));
    foreach (string line in linesToKeep)
    {

        string partialA = line.Substring(9);
        string partialB = partialA.Remove(partialA.Length - 2);
        arrayByte = ToByteArray(partialB);
        using (var stream = new FileStream(fileName+"_gugggu", FileMode.OpenOrCreate))
        {
            FileInfo file = null;
            file = new FileInfo(fileName + "_gugggu");
            stream.Position = file.Length;
            stream.Write(arrayByte, 0, arrayByte.Length);
        }
    }
}
public static byte[] ToByteArray(String HexString)
{
    int NumberChars = HexString.Length;
    byte[] bytes = new byte[NumberChars / 2];
    for (int i = 0; i < NumberChars; i += 2)
    {
        bytes[i / 2] = Convert.ToByte(HexString.Substring(i, 2), 16);
    }
    return bytes;
}

This method is doing what I need but it takes ages to finish, the original files have roughly 70000 lines... Is there a better way to do that in order to increase speed?

这种方法正在做我需要的但是需要很长时间才能完成,原始文件大约有70000行...有没有更好的方法来提高速度?

EDIT SOURCE FILE EXAMPLE:

编辑源文件示例:

:106AD00000000000000000000000000000000000B6
:106AE00000000000000000000000000000000000A6
:106AF0000000000000000000000000000000000096
:106B00000000000000000000000000000000000085
:106B10000000000000000000000000000000000075
:106B20000000000000000000000000000000000065
:106B30000000000000000000000000000000000055
:106B40000000000000000000000000000000000045
:106B50000000000000000000000000000000000035
:106B60000000000000000000000000000000000025

1 个解决方案

#1


3  

It is OK to use File.ReadLines(...) because it actually is shortcut to StreamReader.ReadLine() (Not to be confused with File.ReadAllLines()).

可以使用File.ReadLines(...),因为它实际上是StreamReader.ReadLine()的快捷方式(不要与File.ReadAllLines()混淆)。


I wonder why you reopen the same file for each line you want to save. I would do something like this:

我想知道你为什么要为你要保存的每一行重新打开相同的文件。我会做这样的事情:

  byte[] arrayByte = { 0x00 };

  using (var stream = new FileStream(fileName + "_gugggu", FileMode.Create))
  {
    foreach (string line in File.ReadLines(fileName).Where(l => l.Contains(":10")))
    {
      ...

This may be the real bottleneck.

这可能是真正的瓶颈。


I think

      string partialA = line.Substring(9);
      string partialB = partialA.Remove(partialA.Length - 2);

is the same as:

是相同的:

      string subString = line.Substring(9, line.Length - 11); // 11 = 9 + 2

All in all it could be changed to :

总而言之,它可以改为:

  byte[] arrayByte = { 0x00 };

  using (var stream = new FileStream(fileName + "_gugggu", FileMode.Create))
  {
    foreach (string line in File.ReadLines(fileName).Where(l => l.Contains(":10")))
    {
      string subString = line.Substring(9, line.Length - 9 - 2);
      arrayByte = ToByteArray(subString);
      stream.Write(arrayByte, 0, arrayByte.Length);
    }
  }

#1


3  

It is OK to use File.ReadLines(...) because it actually is shortcut to StreamReader.ReadLine() (Not to be confused with File.ReadAllLines()).

可以使用File.ReadLines(...),因为它实际上是StreamReader.ReadLine()的快捷方式(不要与File.ReadAllLines()混淆)。


I wonder why you reopen the same file for each line you want to save. I would do something like this:

我想知道你为什么要为你要保存的每一行重新打开相同的文件。我会做这样的事情:

  byte[] arrayByte = { 0x00 };

  using (var stream = new FileStream(fileName + "_gugggu", FileMode.Create))
  {
    foreach (string line in File.ReadLines(fileName).Where(l => l.Contains(":10")))
    {
      ...

This may be the real bottleneck.

这可能是真正的瓶颈。


I think

      string partialA = line.Substring(9);
      string partialB = partialA.Remove(partialA.Length - 2);

is the same as:

是相同的:

      string subString = line.Substring(9, line.Length - 11); // 11 = 9 + 2

All in all it could be changed to :

总而言之,它可以改为:

  byte[] arrayByte = { 0x00 };

  using (var stream = new FileStream(fileName + "_gugggu", FileMode.Create))
  {
    foreach (string line in File.ReadLines(fileName).Where(l => l.Contains(":10")))
    {
      string subString = line.Substring(9, line.Length - 9 - 2);
      arrayByte = ToByteArray(subString);
      stream.Write(arrayByte, 0, arrayByte.Length);
    }
  }