开发者

How to read a file from the x line in .NET

I read a file from the file system and FTP it to an FTP 开发者_JS百科server. I now have a request to skip the first line (it's a CSV with header info). I think I can somehow do this with the Offset of the stream.Read method (or the write method) but I am not sure how to translate the byte array offset from a single line.

How would I calculate the offset to tell it to only read from the 2nd line of the file?

Thanks

// Read the file to be uploaded into a byte array
        stream = File.OpenRead(currentQueuePathAndFileName);
        var buffer = new byte[stream.Length];
        stream.Read(buffer, 0, buffer.Length);
        stream.Close();

        // Get the stream for the request and write the byte array to it
        var reqStream = request.GetRequestStream();
        reqStream.Write(buffer, 0, buffer.Length);
        reqStream.Close();
        return request;


You should use File.ReadAllLines. It returns array of strings. Then just strArray.Skip(1) will return you all lines except of first.

UPDATE

Here is the code:

var stringArray = File.ReadAllLines(fileName);
if (stringArray.Length > 1)
{
   stringArray = stringArray.Skip(1).ToArray();
   var reqStream = request.GetRequestStream();
   reqStream.Write(stringArray, 0, stringArray.Length);
   reqStream.Close();       
}


If you need just skip the one line, you can use the reader class, read line, get the current position and read contents as usual.

using (var stream = File.OpenRead("currentQueuePathAndFileName"))
using (var reader = new StreamReader(stream))
{
    reader.ReadLine();
    Console.Write(stream.Position);
    var buffer = new byte[stream.Length - stream.Position];
    stream.Read(buffer, 0, buffer.Length);

    var reqStream = request.GetRequestStream();
    reqStream.Write(buffer, 0, buffer.Length);
    reqStream.Close();
    return request;
}

And Why are you using RequestStream? Aren't you supposed to use the HttpContext.Current.Response.OutputStream ?


You could take the string from your stream input, parse it into lines with array split, and then drop the first, and put it back together, OR, you could regex out the first line, or just look for a line feed and copy the string from there on. etc.


You should use a [StreamReader][1] class. Then by using ReadLine until the result is null you read all the lines one by one. Just skeep the first one as your requirement. Reading all the file in memory is usually not a good idea ( it does not scale as the file became bigger )


The brute force approach to is to iterate through the source byte array and look for the first occurrence of the carriage return character followed by a line feed (CR+LF). That way you'll know the offset in the source array to exclude the first line in the CSV file.

Here's an example:

const byte carriageReturn = 13;
const byte lineFeed = 10;
byte[] file = File.ReadAllBytes(currentQueuePathAndFileName);
int offset = 0;

for (int i = 0; i < file.Length; i++)
{
    if (file[i] == carriageReturn && file[++i] == lineFeed)
    {
        offset = i + 2;
        break;
    }
}

Note that this example assumes the source CSV file was created on Windows, since line breaks are expressed differently on other platforms.

Related resources:

  • Environment.NewLine Property
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜