如何读取/写入.txt文件的某些位置 [英] How to read/write in certains positions of a .txt file

查看:66
本文介绍了如何读取/写入.txt文件的某些位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人们..!



我想知道是否有人可以帮我解决这个问题。



所以,我有一个.txt文件,它有大约5000行数字,我需要在某些行中写一些值,在那行中我需要读取某些位置/列,然后再写其他价格在我拿走另一个的地方......我会试着更清楚一点好吧:



我的例子。 txt文件



984896 54 644564564546546546655645664



984984984484 < b> 98 49489



98 4984998489498494894948



和这样,直到文件结束...



所以,你看到粗体数字?那是我需要阅读的,后来在同一个位置写了其他值,我认为它会像更新,但在.txt文件中......:D



所以,我可以使用streamreader读取整个文档,它就像是......



Dim arquivoTexto As String

Dim leitor As StreamReader = File.OpenText(nomeArquivo)



arquivoTexto = leitor.ReadToEnd



然而,我想知道是否有办法从我的.txt文件中读取某些行,而不是整个文件,有点像索引或类似的东西,导致我需要的行读取将永远是相同的,唯一会改变的是值,所以,如果我能得到我想要的那一行,我可以通过一个简单的子串来获取我想要的数字......?



我的问题是......采取行动!!!



问题的第二部分是:如何写入.txt,数字我改变了另一个相同的位置...



例如:

原始.txt文件行0 = 1234 < b> 56 798



。更改行后的.txt文件0 = 1234 99 798



总结,我确切地知道我需要修改的数字的行和列,我只是不知道如何得到那些行/列,并且不知道如何写新数字在正确的位置....:/



***我永远不需要写2个数字然后写3例如,如果我读2,只需2将被修改...



哇,我希望它更可能清除......我确实尝试过了;)



对不起任何英语错误.....我是巴西人,试图学习英语语言:)



既然如此,谢谢你帮助



拥抱4所有

Hi people..!

I´d like to know if somebody could help me with a problem here.

So, I´ve a .txt file and it has around 5000 lines full of numbers, and I need writing in certains lines some values, in that lines I need reading certains positions/columns, and later writing other values at the same place where that I took of the other...I´ll try to be a little more clear ok:

Example of my .txt file

98489654644564564546546546655645664

9849849844849849489

984984998489498494894948

and in that way, till the end of document...

so, you see the bold numbers ? that is the ones that I need read and later write other values at the same position, I think it would be something like an "update", but in a .txt file...:D

So, I can read the entire document using the streamreader, it would be something like....

Dim arquivoTexto As String
Dim leitor As StreamReader = File.OpenText(nomeArquivo)

arquivoTexto = leitor.ReadToEnd

however, I´d like to know if there´s a way to read just certains lines from my .txt file and not the whole file, something that works like a index or kinda of that, cause the lines that I need to read will be always the same, the only thing that will change will be the values, So, if I can get the line that I want, I can take the numbers I want through a simple subString right... ?

my problem is....take that lines !!!

the second part of my problem is: how to write in .txt, the numbers I changed at the same positions where the other have been...

example:
original .txt file line 0 = 123456798

.txt file after alteration line 0 = 123499798

summarizing, I know exactly the lines and columns where the numbers that I need to modify will be, I just dont know how to get that lines/colums, and dont know too how to write the new numbers at the right positions.... :/

*** I never will need writing 2 numbers and then write 3 for example, if I read 2, just 2 will be modified...

Wow, I hope it was the more possible clearing...really I did try ;)

sorry for any english mistakes.....I´m brazilian guy, trying to learn english language :)

Since, thanks any help

hugs 4 all

推荐答案

没有任何功能可以帮助你:文本文件没有结构,行只是插入到字符流中的字符(或两个,具体取决于系统),被应用程序视为行终止字符。

因此,为了找到第123行,你必须阅读到那一点的所有文本并计算新行的数量。

如果你想改变文件中的一个或多个字符,那么这是一回事 - 您可以将流找到适当的位置并写入新值,但如果您想要写更长或更短的字符串,那么您必须阅读整个文件到目前为止,将其写入第二个文件,写入新数据,然后将输入文件的其余部分复制到输出。



最简单的方法做你想要的可能是使用File.ReadAllLines返回一个字符串数组,一个用于文件中的每一行,修改字符串,然后在完成后使用File.WriteAllLines重新生成文件。
There is no function that will help you: text files have no structure, the "lines" are just a character (or two depending on the system) inserted into the character stream which is treated as a line termination character by applications.
So in order to find "line 123", you have to read all the text up to that point and count the number of new lines.
If you want to change a character or characters in a file, then that is one thing - you can SEEK the stream to the appropriate position and write the new values, but if at any point you want to write a longer or shorter string then you have to read the whole file up to that point, writing it to a second file, write your new data, then copy the rest of the input file to the output.

The easiest way to do what you want is probably to use File.ReadAllLines which returns an array of strings, one for each line in the file, modify the strings, and then use File.WriteAllLines to regenerate the file when you are done.


这篇关于如何读取/写入.txt文件的某些位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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