写在文本文件的特殊行上 [英] write on Special line of a text file

查看:75
本文介绍了写在文本文件的特殊行上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i将写在特殊行的文本文件

解决方案

想一想,你的意思是具体行而不是特殊线?

如果是这样,那么你可能会感到失望。

文本文件没有行 - 它们只是一个字符流,其中一些字符被读取它们的软件解释为换行符。你不能只是移动到第102行并将其写下来,因为除非您编写的文本与您要替换的文本的长度完全相同,否则会弄乱文件。



如果您将文本文件组织为等长线,则可以这样做:只需将Seek方法用于字符位置(行号*行长),然后写出正确的字符数,但是如果你还没有,那么你必须从你的文件中读取你想要替换的行的开头,当你转到一个新文件时写,然后在原始文件中跳过你想要替换的文本,将新文本写入输出文件,然后将输入文件的其余部分复制到输出。最后,你可以关闭这两个文件,删除原文并重命名新文件。



如果你真的想处理基于行的信息,那么文本文件很差选择:有更好的解决方案。


WinForms TextBox有一个'Lines属性,返回一个只读字符串数组。有趣的是,'Lines Property,作为一个整体,可以分配给。



修改TextBox的Lines数组中的任何一行Text:

  private   void  ModifyLine(TextBox targetTB, int  lineToChange, string  newLineContent)
{
// 检查提供的索引
如果(lineToChange > targetTB.Lines.Length - 1
throw new ArgumentOutOfRangeException

lineToChange
TextBox的Lines集合的索引无效: + targetTB.Name
);

// 创建TextBox的Lines数组的副本
// 可以修改(写入)
string [] lineAry = new string [targetTB.Lines.Length];
targetTB.Lines.CopyTo(lineAry, 0 );

// 更改指定行的内容
lineAry [lineToChange ] = newLineContent;

// 更新目标TextBox的Lines数组
targetTB。 Lines = lineAry;
}

调用'ModifyLine:

 ModifyLine(textBox1,2,修改后的第3行)的示例; 


hi
i will write on Special line of text file

解决方案

Thinking about it, do you mean "Specific Line" rather than "Special Line"?
If so, then you may be disappointed.
Text files do not have "lines" as such - they are just a stream of character, where some of the characters are interpreted as line breaks by the software that reads them. You can't just "move to line 102" and write over it, because unless the text you write is exactly the same length as the text you are replacing, it will mess up the file.

If you have organised your text file as equal length lines then you can do it: just use the Seek method to a character position (line number * length of line) then write the correct number of characters, but if you haven't, then you you will have to read from your file up to the start of the line you want to replace, writing as you go to a new file, then skip the text you wish to replace in the original file and write the new text to the output file instead, then copy the rest of the input file to the output. finally, you can close both files, delete the original and rename the new file.

If you really want to deal with row based information, a text file is a poor choice: there are much better solutions out there.


The WinForms TextBox has a 'Lines Property that returns a readonly array of strings. Interestingly, the 'Lines Property, as a whole, can be assigned to.

To modify any one line of Text in a TextBox's Lines array:

private void ModifyLine(TextBox targetTB, int lineToChange, string newLineContent)
{
    // check the supplied index
    if (lineToChange > targetTB.Lines.Length - 1) 
      throw new ArgumentOutOfRangeException
      (
       "lineToChange",
       "invalid index to Lines collection of TextBox: " + targetTB.Name
      );

    // create a copy of the TextBox's Lines array
    // that can be modified (written to)
    string[] lineAry = new string[targetTB.Lines.Length];
    targetTB.Lines.CopyTo(lineAry, 0);

    // change the specified line's content
    lineAry[lineToChange] = newLineContent;

    // update the target TextBox's Lines array
    targetTB.Lines = lineAry;
}

A sample of a call to 'ModifyLine:

ModifyLine(textBox1, 2, "modified line 3");


这篇关于写在文本文件的特殊行上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆