tags:

views:

319

answers:

3

Hi all

Am I missing something or does System.IO.FileStream not read Unicode text files containing Hebrew?

    public TextReader CSVReader(Stream s, Encoding enc) 
    {

        this.stream = s;
        if (!s.CanRead) 
        {
            throw new CSVReaderException("Could not read the given CSV stream!");
        }
        reader = (enc != null) ? new StreamReader(s, enc) : new StreamReader(s);
    }

Thanks Jonathan

+5  A: 

The FileStream is nothing but a byte stream, which is language/charset agnostic. You need an encoding to convert bytes into characters (including Hebrew) and back.

There are several classes to help you with that, the most important being System.Text.Encoding and System.IO.StreamReader and System.IO.StreamWriter.

Lucero
@Lucerno: he edited his post, that's what he's using.
Hans Passant
+2  A: 

The stream might be closed.

From msdn on CanRead:

If a class derived from Stream does not support reading, calls to the Read, ReadByte, and BeginRead methods throw a NotSupportedException.

If the stream is closed, this property returns false.

Zach Johnson
A: 

I'd wager that you're simply not using the right encoding. Chances are you're passing in Encoding.Default or Encoding.ASCII when you should actually be passing Encoding.UTF8 (most common) or Encoding.Unicode to that method.

If you're sure that you're using the right encoding, post the full code and an example of the file.

Aaronaught