tags:

views:

123

answers:

7

I need to write contents of a file to another file using File.OpenRead and File.OpenWrite methods. I am unable to figure out how to do it.

How can i modify the following code to work for me.

using (FileStream stream = File.OpenRead("C:\\file1.txt"))
using (FileStream writeStream = File.OpenWrite("D:\\file2.txt"))
{
       BinaryReader reader = new BinaryReader(stream);
       BinaryWriter writer = new BinaryWriter(writeStream);
       writer.Write(reader.ReadBytes(stream.Length));
}
+1  A: 

Use FileStream class, from System.IO.

[ComVisibleAttribute(true)]
public class FileStream : Stream
Svisstack
my code sample uses filestream. How should i modify this code to work?
Sumee
@Summe: you dont using it.
Svisstack
+1  A: 

Try something along these lines:

using (FileStream input = File.OpenRead(pathToInputFile),
    output = File.OpenWrite(pathToOutputFile))
{
    int read = -1;
    byte[] buffer = new byte[4096];
    while (read > 0)
    {
        read = input.Read(buffer, 0, buffer.Length);
        output.Write(buffer, 0, read);
    }
}

Note that this is somewhat 'skeletal' and you should amend as required for your application of it.

Mr. Disappointment
Why do u say, i shoould change it as required by my application? Do u mean to say i need to decide the chunk size (which in your case is 4mb)? Should we really read bytes in chunks or can do it in one go?
Sumee
The byte size is 4k not 4mb.
Wes Price
@Sumee I imagine that the reading it all in one go, is dependent on your filesize, if you have a really massive file, perhaps reading all could be too large for your buffer? (I don't know what happens, other then I suspect you might not get all the text as you expect)
onaclov2000
@Wes Price - sorry i meant 4k
Sumee
@Summe: Yes, change the file paths and buffer length to what is required. Reading in increments could benefit you, as onaclov2000 mentions, if you have a large file then there are a couple of symptoms which may occur which this approach could help you mitigate.
Mr. Disappointment
@Eugene: I believe the offset would be used to specify an offset from the current position in either file, we, however, do not require any further offset as the location we are at in either file is dictated by how many bytes we last read or have written.
Mr. Disappointment
A: 

Is it necessary to us FileStream? Because you can do this very easily with simple File Class like;

using System.IO;
string FileContent = File.ReadAllText(FilePathWhoseTextYouWantToCopy);
File.WriteAllText(FilePathToWhomYouWantToPasteTheText,FileContent);
KhanZeeshan
A: 

Have you checked that the reader is reading all the data? This MSDN page has an example that checks all the data is read:

    byte[] verifyArray = binReader.ReadBytes(arrayLength);
    if(verifyArray.Length != arrayLength)
    {
        Console.WriteLine("Error reading the data.");
        return;
    }

The other alternative is that you probably need to Flush the output buffer:

writer.Flush();
ChrisF
+1  A: 
using (var inputStream = File.OpenRead(@"C:\file1.txt"))
{
    using (var outputStream = File.OpenWrite(@"D:\file2.txt"))
    {
        int bufferLength = 128;
        byte[] buffer = new byte[bufferLength];
        int bytesRead = 0;

        do
        {
            bytesRead = inputStream.Read(buffer, 0, bufferLength);
            outputStream.Write(buffer, 0, bytesRead);
        }
        while (bytesRead != 0);
    }
}
+2  A: 
    using (FileStream stream = File.OpenRead("C:\\file1.txt"))
    using (FileStream writeStream = File.OpenWrite("D:\\file2.txt"))
    {
        BinaryReader reader = new BinaryReader(stream);
        BinaryWriter writer = new BinaryWriter(writeStream);

        // create a buffer to hold the bytes 
        byte[] buffer = new Byte[1024];
        int bytesRead;

        // while the read method returns bytes
        // keep writing them to the output stream
        while ((bytesRead =
                stream.Read(buffer, 0, 1024)) > 0)
        {
            writeStream.Write(buffer, 0, bytesRead);
        }
    }

Just wonder why not to use this:

File.Copy("C:\\file1.txt", "D:\\file2.txt");
Pavlo Neyman
A: 

You should be using File.Copy unless you want to append to the second file.

If you want to append you can still use the File class.

string content = File.ReadAllText("C:\\file1.txt");
File.AppendAllText("D:\\file2.txt",content);
Ali YILDIRIM