文本文件处理 [英] Text file Handling

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

问题描述

您好,需要能够创建一个存储信息的文本文件:

这是我用流编写器管理/追加


1

aaaa

bbbbb

ccccccc

dddddddd

---- --------------

2

aaaa

bbbbb

cccccc

dddddddd


我的问题是现在我必须编辑文本文件中的单独一行。

例如用户输入数字2。在文本框中以验证他们希望访问哪条记录。 (流读取器需要找到该记录)然后,用户需要更改bbbbb的条目细节。到xxxxx仅适用于该记录。


有关如何在文件中搜索标识号的任何想法,例如: 2然后在该记录中修改第二行。


提前感谢

解决方案


您好,需要能够创建一个存储信息的文本文件:

这是我用流编写器/附加管理


1

aaaa

bbbbb

ccccccc

dddddddd

- -----------------

2

aaaa

bbbbb

cccccc

dddddddd


我的问题是现在我必须编辑文本文件中的单独一行。

例如用户输入数字2。在文本框中以验证他们希望访问哪条记录。 (流读取器需要找到该记录)然后,用户需要更改bbbbb的条目细节。到xxxxx仅适用于该记录。


有关如何在文件中搜索标识号的任何想法,例如: 2,然后修改该记录中的第二行。


提前感谢



你检查过MSDN吗? />
有一篇关于如何的文章从文件中读取文本。它告诉你如何从文本文件中获取行...


一种可能的解决方案是从文本文件中获取所有行并将它们添加到ArrayList中。然后,根据用户输入的内容,编辑该String(在该索引数组处)...完成编辑后,将文本文件与ArrayList的内容一起编写....


还有很多其他方法可以解决这个问题。

尝试一下,如果你遇到更具体的问题,请回复我们。


-Frinny


我们是在谈论DataTable的平面文件存储格式吗?


我可能会创建一个x列的DataTable,其中第一个是主键,其余项目(在-------之前)是其他字段。然后我将创建一个具有相同数量列的DataRow,将每个项目(在-------之前)解析为DataRow中的一个字段。每个DataRow完成后,我都会将它添加到DataTable中。一旦所有数据都在DataTable中,它就更容易查询,从而使得进一步编码更简单,更高效,因为您现在正在查询内存,而不是每次读取都转发到磁盘或从磁盘中继。


我的保存将迭代DataTable将每个DataRow写回文件...您可以选择只写入已更改的行/字段,或者您可以编写整个文件...取决于文件大小和更改的数据量取决于您可能选择的方法。如果数据量很小或者每次都有大部分数据发生变化,那么将整个表格写回文件的效率可能更高,否则您可能希望只写下已更改的字段。 />

只是我谦虚的2美分...如果这纯粹是一个文件读/写练习,那么我很可能会为你解决这个问题 - 即使从代码角度来看它也是如此更加优雅/高效。



一种可能的解决方案是从文本文件中取出所有行并将它们添加到一个ArrayList。然后,根据用户输入的内容,编辑该String(在该索引数组处)...完成编辑后,将文本文件与ArrayList的内容一起编写....

-Frinny



这是我想到的想法,抓住文本文件中的行并将它们放入数组中。


所以你建议阅读整个文本文件并将其转换成arraylist吗?



感谢Balablaster,但是这是一个简单的阅读/写/修改任务。

我试图用一个非常简单的迷你数据库来存储使用文本文件的记录



这就是我之前想要做的事情,虽然它现在显然很垃圾,但我会试着给出一个arraylist的想法:


展开 < span class =codeDiv ider> | 选择 | Wrap | 行号

Hi there, need to be able to create a text file that stores information like so :
this i have managed with a stream writer/append

1
aaaa
bbbbb
ccccccc
dddddddd
------------------
2
aaaa
bbbbb
cccccc
dddddddd

My problem is now i have to "Edit" an individual line within the text file.
e.g. The user enters number "2" in to a text box to verify which record they wish to access. (the streamreader needs to locate that record) The user then needs to change the entry details for "bbbbb" to "xxxxx" for that record only.

Any ideas on how to search a file for the identification number e.g. 2 and then modify the 2nd line down within that record.

thanks in advance

解决方案

Hi there, need to be able to create a text file that stores information like so :
this i have managed with a stream writer/append

1
aaaa
bbbbb
ccccccc
dddddddd
------------------
2
aaaa
bbbbb
cccccc
dddddddd

My problem is now i have to "Edit" an individual line within the text file.
e.g. The user enters number "2" in to a text box to verify which record they wish to access. (the streamreader needs to locate that record) The user then needs to change the entry details for "bbbbb" to "xxxxx" for that record only.

Any ideas on how to search a file for the identification number e.g. 2 and then modify the 2nd line down within that record.

thanks in advance


Have you checked MSDN?
There''s an article on How To Read Text From A File. It tells you how to grab the lines from the text file...

One possible solution would be to take all of the lines from the text file and add them to an ArrayList. Then, depending on what the user has entered, edit that String (at that index array)...after your finished editing, over write the text file with the contents of the ArrayList....

There are many other ways to solve this problem.
Give it a try and then get back to us if you get stuck on something more specific.

-Frinny


Are we talking a flat file storage format of a DataTable?

I''d probably create a DataTable of x columns, where the first is the primary key and the remaining items (before the -------) are the other fields. Then I''d create a DataRow of the same amount of columns parsing each item (before the -------) into one of the fields in the DataRow. As each DataRow is completed, I''d add it to the DataTable. Once all of the data is in the DataTable it''s far easier to query and thus making further coding simpler and more efficient as you are now querying memory instead of relaying to/from disk for each read.

My save would then iterate the DataTable writing each DataRow back to the file...you could choose just to write the rows/fields that have changed, or you could write the whole file... depending on the file size and how much data has changed depends on which method you''re likely to choose. If the amount of data is small or a large portion of the data changes each time, then it''s mostly likely more efficient to write the whole table back to the file, otherwise you may wish to just write the changed fields.

Just my humble 2 cents... if this is purely a file read/write exercise, then I''m most likely complicating the issue for you - even though from a code standpoint it''s far more elegant/efficient.


One possible solution would be to take all of the lines from the text file and add them to an ArrayList. Then, depending on what the user has entered, edit that String (at that index array)...after your finished editing, over write the text file with the contents of the ArrayList....
-Frinny

This was the idea i had in mind, grabbing the lines from the text file and putting them in to an array.

So do you recommend reading the entire text file and turning it in to an arraylist?



Thanks Balablaster but yes it is a simple read/write/ammend task.
im trying to make a very simple mini database to store records using a text file



This is what i was trying to do before, although it now clearly rubbish, I will try and give the arraylist idea a go:



Expand|Select|Wrap|Line Numbers


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

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