开发者

How to write contents of one file to another file?

开发者 https://www.devze.com 2023-01-19 12:09 出处:网络
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.

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.OpenRea开发者_如何学Pythond("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));
}


    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");


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);

This works for file with small size as entire file in loaded into the memory.


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.


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);


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);
    }
}


Use FileStream class, from System.IO.

[ComVisibleAttribute(true)]
public class FileStream : Stream


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();


If you are not keen at using Read/Write function of File , you can better try using Copy functionality

Easiest will be : 

      File.Copy(source_file_name, destination_file_name, true)

true--> for overwriting existing file,without "true" it will create a new file.But if the file already exists it will throw exception without "true" argument.

0

精彩评论

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