如何一次读取和写入文件. [英] How to Read and Write to a file at a time.

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

问题描述

大家好

我正在尝试一次读取和写入同一文件,但它显示出这样的错误

该进程无法访问文件``D:\ sample2.txt'',因为它正在被另一个进程使用.

 使用(StreamReader sr =  StreamReader(  D:\ sample1.txt"))
            {
                使用(StreamReader srn =  StreamReader( @"  D:\ sample2.txt"))
                {
                    使用(StreamWriter sw =  StreamWriter( @"  D:\ sample2.txt"))
                    {
                        同时(!sr.EndOfStream)
                        {
                            字符串 actualURL = sr.ReadLine();
                             while (!srn.EndOfStream)
                            {
                                字符串 uniqueURL = srn.ReadLine();
                                如果(!actualURL.Equals(uniqueURL))
                                {
                                    sw.WriteLine(uniqueURL);
                                }
                            }
                        }
                    }
                }
            } 



如何解决这个问题????

我的要求:
在Sample1文本文件中,存在一些重复的URL,我想取出那些重复的URL.

Sample2文本文件不包含任何内容

我的方法:
我正在检查Sample2文本文件中是否存在此字符串
如果不存在,则将其写入sample2文本文件,否则不提供.

但这表明了这种类型的错误

请帮助解决问题,如果我有任何错误,请提供身份

在此先感谢

解决方案

您无法在同一文件上打开读者和作家!
您可以通过打开一个文件进行读写访问来完成此操作,因为可以将其指定为非排他锁.但是,尝试完全执行您的操作可能会导致大问题:您需要输出到第三个文件,然后

 使用(StreamReader sr =  StreamReader( @" 跨度>))
            {
                同时(!sr.EndOfStream)
                {
                    FileStream fileStream =  FileStream( @" ,FileMode.Open,FileAccess.ReadWrite,FileShare.Read);
                    使用(StreamReader srn =  StreamReader(fileStream))
                    {
                        使用(TextWriter sw =  StreamWriter(fileStream))
                        {
                            字符串 actualURL = sr.ReadLine();

                             while (!srn.EndOfStream)
                            {
                                字符串 uniqueURL = srn.ReadLine();
                                如果(字符串.比较(actualURL,uniqueURL, false )==  0 )
                                {
                                    sw.WriteLine(actualURL);
                                }
                            }
                        }
                    }
                }
            } 



尝试上面的代码..

OriginalGriff也是写的


检查此内容.

http://www.c-sharpcorner.com/UploadFile/dbeniwal321 /file-openwrite-method-in-C-Sharp/ [using (StreamReader sr = new StreamReader(@"D:\sample1.txt")) { using (StreamReader srn = new StreamReader(@"D:\sample2.txt")) { using (StreamWriter sw = new StreamWriter(@"D:\sample2.txt")) { while (!sr.EndOfStream) { string actualURL = sr.ReadLine(); while (!srn.EndOfStream) { string uniqueURL = srn.ReadLine(); if (!actualURL.Equals(uniqueURL)) { sw.WriteLine(uniqueURL); } } } } } }



How to solve this issue???

My Requirement:
In the Sample1 text file some repeated URLs present, i want to take out that repeated URLs.

Sample2 text file does not contain any thing

My approach:
I am checking that this string is present in the Sample2 text file or not
if not present than write in the sample2 text file else not.

But this is showing this type of error

Please help how to solve the problem and please identity if i have done any mistake

Thanks in advance

解决方案

You can''t open a reader and a writer on the same file!
You can do it by opening a file for read-write access, becasue that can be specified as a nonexclusive lock., but trying to do exactly what you are doing will probably cause big problems: You need to output to a third file, then rename it at the end after deleting the second.


using (StreamReader sr = new StreamReader(@"D:\sample1.txt"))
            {
                while (!sr.EndOfStream)
                {
                    FileStream fileStream = new FileStream(@"D:\sample2.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
                    using (StreamReader srn = new StreamReader(fileStream))
                    {
                        using (TextWriter sw = new StreamWriter(fileStream))
                        {
                            string actualURL = sr.ReadLine();

                            while (!srn.EndOfStream)
                            {
                                string uniqueURL = srn.ReadLine();
                                if (string.Compare(actualURL,uniqueURL,false) == 0)
                                {
                                    sw.WriteLine(actualURL);
                                }
                            }
                        }
                    }
                }
            }



Try the above code..

OriginalGriff is also write


Check this.

http://www.c-sharpcorner.com/UploadFile/dbeniwal321/file-openwrite-method-in-C-Sharp/[^]


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

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