如何从特定txt文件中的特定行更改特定对象 [英] How can I change a specific object from specific line in specific txt file

查看:78
本文介绍了如何从特定txt文件中的特定行更改特定对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有医院自动化代码,在删除部分我需要更改有关患者的特定信息而不删除整行。



什么我试过了:



i试过:

 var file = new List< string>(系统.IO.File.ReadAllLines( Hasta.txt)); // ArananTc'yegöretizilerkullanarakHastadosya'sındabilgilerisil file.RemoveAt(i); File.WriteAllLines(Hasta.txt,file.ToArray()); 

解决方案

文本文件没有行 - 它们具有行终止符,允许您在某些情况下将它们视为单独的行。但这并不意味着线条是线条 - 如果你的新数据是比旧数据更大或更小的一个字符(它会是),那么你不能只是重写一条线,就像你有从文件的整个其余部分插入或删除字符来执行此操作。



文本文件在这里不是一个好选择 - 更新它们的唯一安全方法是将文件读取到要更改的记录,然后将其写入新文件,然后编写新信息,跳过旧行,并读取文件的其余部分,将其写入新文件。完成后,关闭两个文件,删除原始文件,然后重命名新文件。



另一种选择是阅读所有文本 - 就像你一样 - 然后改变您的记录并将其全部写回新文件。然后删除旧的并重命名新的。



但是......医院从来都不是单一用户环境,所以这充满了危险并且很有可能丢失数据结果。所有需要的是两个用户同时尝试更改不同的记录,有人会失去他们输入的内容!



相反,考虑使用数据库 - Sql服务器或MySql是不错的选择 - 让它自己处理文件,而你只需告诉它要插入什么,要更新什么等等 - 当你运行代码时它也会很多,速度要快得多!


没有看到文本文件的内容,我建议从基础开始:

如何:从文件中读取文本 [ ^ ]

如何:将文本写入文件 [ ^ ]



但我觉得你需要使用 XmlSerialization [< a href =https://msdn.microsoft.com/en-us/library/182eeyhh(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]

I have a Hospital automation code and at the delete section i need to change a specific information about the Patient without delete the whole line.

What I have tried:

i tried :

var file = new List<string>(System.IO.File.ReadAllLines("Hasta.txt")); //Aranan Tc'ye göre diziler kullanarak Hasta dosya'sında bilgileri sil file.RemoveAt(i); File.WriteAllLines("Hasta.txt", file.ToArray());

解决方案

Text files don't have "lines" - they have "line terminator characters" which allow you to treat them as separate lines under some circumstances. But that doesn't mean that "lines are lines" - if your new data is one character bigger or smaller than the old data (and it will be) then you can't just "re-write a line", as you have to insert or delete character form the entire rest of the file to do that.

Text files are not a good choice here - the only safe way to update them is the read the file up to the record you want to change, writing it to a new file as you go, then write the new information, skip the old line, and read the rest of the file, writing that to the new file. When you are finished, close both files, delete the original, and rename the new.

The other alternative is to read all the text - as you are - then change your record and write it all back as a new file. Then delete the old and rename the new.

But ... hospitals are never single user environments so this is fraught with danger and has a high risk of losing data as a result. All it take is two users trying to change different records at the same time, and somebody is going to lose what they entered!

Instead, consider using a database - Sql Server or MySql are good choices - and let it handle the files themselves, while you just tell it what to insert, what to update, and so on - it'll also be a lot, lot faster when you come to run the code!


Without seeing the content of text file, i'd suggest to start with basics:
How to: Read Text from a File[^]
How to: Write Text to a File[^]

But i have a feeling that you need to use XmlSerialization[^]


这篇关于如何从特定txt文件中的特定行更改特定对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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