简体   繁体   English

内存不足异常读写文本文件

[英]Out of memory exception reading and writing text file

I get an out of memory exception a few seconds after I execute the following code.执行以下代码几秒钟后,我收到内存不足异常。 It doesn't write anything before the exception is thrown.在抛出异常之前它不会写任何东西。 The text file is about half a gigabyte in size.文本文件的大小约为 0.5 GB。 The text file I'm writing too will end up being about 3/4 of a gigabyte.我正在编写的文本文件最终将占用大约 3/4 GB 的空间。 Are there any tricks to navigate around this exception?有什么技巧可以绕过这个异常吗? I assume it's because the text file is too large.我认为这是因为文本文件太大。

public static void ToCSV(string fileWRITE, string fileREAD)
{
    StreamWriter commas = new StreamWriter(fileWRITE);
    var readfile = File.ReadAllLines(fileREAD);


    foreach (string y in readfile)
    {

        string q = (y.Substring(0,15)+","+y.Substring(15,1)+","+y.Substring(16,6)+","+y.Substring(22,6)+ ",NULL,NULL,NULL,NULL");
        commas.WriteLine(q);
    }

    commas.Close();
}

I have changed my code to the following yet I still get the same excpetion?我已将代码更改为以下代码,但仍然得到相同的异常?

public static void ToCSV(string fileWRITE, string fileREAD)
{
    StreamWriter commas = new StreamWriter(fileWRITE);

    using (FileStream fs = File.Open(fileREAD, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    using (BufferedStream bs = new BufferedStream(fs))
    using (StreamReader sr = new StreamReader(bs))
    {
        string y;
        while ((y = sr.ReadLine()) != null)
        {
            string q = (y.Substring(0, 15) + "," + y.Substring(15, 1) + "," + y.Substring(16, 6) + "," + y.Substring(22, 6) + ",NULL,NULL,NULL,NULL");
            commas.WriteLine(q);
        }
    }

    commas.Close();
}

In the following post you can find numerous methods for reading and writing large files.在下面的文章中,您可以找到多种读取和写入大文件的方法。 Reading large text files with streams in C# 在 C# 中使用流读取大型文本文件

Basically you just need to read the bytes in a buffer which will be reused.基本上,您只需要读取将被重用的缓冲区中的字节。 This way you will load a very small amount of the file into memory.这样您就可以将非常少量的文件加载到内存中。

Instead of reading the whole file, try to read and process line by line .不要读取整个文件,而是尝试逐行读取和处理 That way you do not risk to run into an out of memory exception.这样您就不会冒遇到内存不足异常的风险。 Because even if you succeed in organising more memory for the program, one day will come where the file will again be too large.因为即使您成功地为程序组织了更多内存,总有一天文件会再次变得太大。

But the program may loose speed if you use less memory, so basically you'd have to balance the memory use and the execution time.但是如果您使用较少的内存,程序可能会降低速度,因此基本上您必须平衡内存使用和执行时间。 One workaround would be to use a buffered output, reading more than one line at a time or transforming the strings in multiple threads.一种解决方法是使用缓冲输出,一次读取多于一行或在多个线程中转换字符串。

Read the file line by line, it will help you to avoid OutOfMemoryException .逐行阅读文件,它将帮助您避免OutOfMemoryException And I personnaly prefer using using 's to handle streams.我个人更喜欢使用using来处理流。 It makes sure that the file is closed in case of an exception.它确保在出现异常时关闭文件。

public static void ToCSV(string fileWRITE, string fileREAD)
{
    using(var commas = new StreamWriter(fileWRITE))
    using(var file = new StreamReader("yourFile.txt"))
    {
        var line = file.ReadLine();

        while( line != null )
        { 
            string q = (y.Substring(0,15)+","+y.Substring(15,1)+","+y.Substring(16,6)+","+y.Substring(22,6)+ ",NULL,NULL,NULL,NULL");
            commas.WriteLine(q);
            line = file.ReadLine();
        }
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM