0

How can I create a clone object of streamreader, when i do Serializing on streamreader object, program give me exception :

Unhandled Exception: System.Runtime.Serialization.SerializationException: Type ' System.IO.FileStream' in Assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, is not marked as serializable.

how can i do this?

suppose i have text file like with the text:

 1
 2   
 3    
 4
 5

my program:

 [Serializable()]
class Program   
{

    static void Main(string[] args)
    {
        Program obj = new Program();
        obj.read();
    }
    void read()
    {

        StreamReader reader1 = new StreamReader(@"d:\test.txt");
        string s = reader1.ReadLine();
        Console.WriteLine(s);
       SerializeObject("text.txt", reader1);
       StreamReader reader2;
        for (int i = 0; i < 3; i++)
        {
            reader1.ReadLine();
        }
        s = reader1.ReadLine();
        Console.WriteLine(s);
        reader2 = DeSerializeObject("text.txt");
        s = reader2.ReadLine();
        Console.WriteLine(s);
    }
    public void SerializeObject(string filename, StreamReader objectToSerialize)
    {
        Stream stream = File.Open(filename, FileMode.Create);
        BinaryFormatter bFormatter = new BinaryFormatter();
        bFormatter.Serialize(stream, objectToSerialize);
        stream.Close();
    }
    public StreamReader DeSerializeObject(string filename)
    {
        StreamReader objectToSerialize;
        Stream stream = File.Open(filename, FileMode.Open);
        BinaryFormatter bFormatter = new BinaryFormatter();
        objectToSerialize = (StreamReader)bFormatter.Deserialize(stream);
        stream.Close();
        return objectToSerialize;
    }
}

I want to output must be:

1
5
2
Rohit
  • 52
  • 6
  • 2
    Why do you want to clone the reader? – tvanfosson Jul 08 '14 at 04:01
  • i read a text file line by line and match with another file if a line match then i want to store that position of object and continue reading with this object – Rohit Jul 08 '14 at 04:05
  • 1
    Serializing a StreamReader doesn't make sense because the StreamReader doesn't contain the actual data. Why are you trying to serialize the StreamReader itself? Usually, you want to store some kind of *data*, not the *mechanism* used to read it. – Moshe Katz Jul 08 '14 at 04:05
  • you know another way to do this? – Rohit Jul 08 '14 at 04:07
  • Once you have the positions (or is it the actual contents you need to save), what are you going to do with it? It wouldn't be hard to keep track of which lines or even the sections of the files that overlap using line numbers or byte offsets. – tvanfosson Jul 08 '14 at 04:10
  • You'd be better off using just the stream and keeping track of the byte offsets, then rewinding (seeking) back to the previous position and continuing. – tvanfosson Jul 08 '14 at 04:37
  • but i don't want to first object change it's last position – Rohit Jul 08 '14 at 04:44
  • Can you give an example of of what the second file looks like that would generate the output in your post? – Mike Hixson Jul 08 '14 at 05:03
  • in this code example i'm showing only demo code in this firstly i make a streamreader object "reader1" then read one line and save the object with serialization after read three line from object "reader1" i deserialize the save object and again start reading with "reader2" object – Rohit Jul 08 '14 at 05:10
  • 1
    Serialization is the process of converting your object into a format that can be easily stored. If you have a class with a bunch of properties, you can serialize it to disk by converting it into a format you prefer (xml, binary, protobuf, etc.). `StreamReader` and `StreamWriter`, on the other hand, are classes which provide a way to read or write sequences of characters. "Serializing a `StreamReader`" doesn't make sense, because a `StreamReader` itself doesn't contain data you would want to persist. And if your data is stored as *text*, you certainly don't need a `BinaryFormatter` (*binary*). – vgru Jul 08 '14 at 06:09
  • ok get it,but how can i do this @Groo – Rohit Jul 08 '14 at 06:45

1 Answers1

1

OK, so you are simply trying to read a file and write to a different file. There is no serialization involved in what you are trying to do. Serialization generally involves persisting objects.

I believe this is what you are after:

static void Main()
{
    using(StreamReader reader = new StreamReader(@"d:\input.txt"))
    using(StreamWriter writer = new StreamWriter(@"d:\output.txt"))
    {
        string line;

        // Write 1st line
        line = reader.ReadLine();
        writer.WriteLine(line);

        // Skip 3 lines
        for (int i = 0; i < 3; i++)
        {
            reader.ReadLine();
        }

        // Write 5th & 6th line
        for (int i = 0; i < 2; i++)
        {
            line = reader.ReadLine();
            writer.WriteLine(line);
        }
    }
}

UPDATE Write the first line, then the fifth line, then the second line:

static void Main()
{
    using(StreamReader reader = new StreamReader(@"d:\input.txt"))
    using(StreamWriter writer = new StreamWriter(@"d:\output.txt"))
    {
        string line;

        // Write first line
        line = reader.ReadLine();
        writer.WriteLine(line);

        // Read the second line
        string second = reader.ReadLine(); ;

        // Skip 3rd & 4th lines
        for (int i = 0; i < 2; i++)
        {
            reader.ReadLine();
        }

        // Write 5th line
        line = reader.ReadLine();
        writer.WriteLine(line);

        // Write the 2nd line
        writer.WriteLine(second);
    }
}
Mike Hixson
  • 5,071
  • 1
  • 19
  • 24