Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read file.inputstream twice

Tags:

c#

asp.net

I need to read csv file twice. but after first reading:

using (var csvReader = new StreamReader(file.InputStream))
{
    fileFullText += csvReader.ReadToEnd();
    file.InputStream.Seek(0, SeekOrigin.Begin);
    csvReader.Close();
}

using file in enother function:

public static List<string> ParceCsv(HttpPostedFileBase file)
{
    //file.InputStream.Seek(0, SeekOrigin.Begin);
    using (var csvReader = new StreamReader(file.InputStream))
    {
       // csvReader.DiscardBufferedData();
       // csvReader.BaseStream.Seek(0, SeekOrigin.Begin);
        string inputLine = "";
        var values = new List<string>();

        while ((inputLine = csvReader.ReadLine()) != null)
        {
            values.Add(inputLine.Trim().Replace(",", "").Replace(" ", ""));
        }
        csvReader.Close();
        return values;
    } 
}

The file.Length is 0. Can anybody help?

like image 360
freethinker Avatar asked Dec 11 '11 07:12

freethinker


People also ask

Can I read InputStream twice?

Don't read it twice. Save the result in a data structure.

Can you read a stream twice C#?

You can not read the input stream twice, even if you will try to set the position, it will give error. What you have to do is to create a new stream and use it as many times as you want: MemoryStream newStream = new MemoryStream(); // CopyTo method used to copy the input stream into newStream variable.

Can we open the output stream twice in java?

As Java docs say clearly, “A stream should be operated on (invoking an intermediate or terminal stream operation) only once. This rules out, for example, “forked” streams, where the same source feeds two or more pipelines, or multiple traversals of the same stream.

Is InputStream read blocking?

According to the java api, the InputStream. read() is described as: If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.


1 Answers

The reason is that SteramReader's Dispose() method also closes the underlying stream; In your case file.InputStream. The using statement calls Dispose() implicitly. Try to replace using with disposes of both your StreamReaded-s after you finished both read operations. As I remember some stream classes have a bool option to leave underlying stream open after dispose.

.NET 4.5 fixed this issue by introducing leaveOpen parameter in SteamReader constructor. See: MSDN

public StreamReader(
    Stream stream,
    Encoding encoding,
    bool detectEncodingFromByteOrderMarks,
    int bufferSize,
    bool leaveOpen
)

One more thing. You do not need to close SteramReader yourself (the line with csvReader.Close();) when you wrap it in using statement, thus Dispose() and Close() are the same in case of StreamReader.

like image 74
George Mamaladze Avatar answered Oct 06 '22 06:10

George Mamaladze