【问题标题】:How to read and write .img files using FileStream in c#?如何在 c# 中使用 FileStream 读写 .img 文件?
【发布时间】:2020-03-21 11:17:04
【问题描述】:

我正在尝试读取图像文件并将其写入新文件。但是写入的图像文件不是受支持的文件。请告诉我读/写图像文件的正确方法是什么。帮帮我!!

这是我的完整代码。 而且我没有收到任何错误。

using System;
using System.IO;

namespace readfile
{
    class Program
    {
        static int totalbyte = 0;
        static void Main(string[] args)
        {
            string path = "C:/Users/Nitesh Rathi/Downloads/"; // file's path.
            string filename = "IMG_20200317_150302.jpg"; // file's name.
            string fullpath = path + filename;

            readfile(fullpath);
            writefile(filename);

            Console.ReadKey(false);
        }

        static void readfile(string path)
        {
            FileStream stm = File.Open(path, FileMode.Open); // open a file using filestream.
            int size = (int)stm.Length; // size of the file.
            byte[] data = new byte[stm.Length]; // file buffer.

            while (size > 0) // loop until file' size is not 0.
            {
                int read = stm.ReadByte(); // reading file's data.
                size -= read;
                totalbyte += read;
            }
        }
        static void writefile(string filename)
        {
            FileStream stm = File.Create(filename); // create a new file.
            byte[] data = new byte[totalbyte]; // file's data.

            Console.WriteLine("Writing data into file...");

            stm.Write(data, 0, data.Length); // writing data into created file.

            Console.WriteLine("data has been wrote into file.");
            stm.Close();
        }
    }
}

我也使用了FileStream.Read() 方法。但它也不适合我。

【问题讨论】:

  • 您的目标只是“复制”现有文件吗?如果是这样,您可以简单地使用 File.Copy 方法,而不是自己构建它。
  • 您正在将所有零写入文件,因为您只执行新字节 [totalbyte]。在关闭之前做一个 stm.Flush();
  • @jdweng 你能告诉我正确的方法是什么吗?
  • 试试System.IO.File.Copy(fullpath, filename);
  • 即使已修复,您的代码仍试图以非常低效的方式逐字节复制文件,并且还将所有文件加载到内存中。请不要这样做。您的解决方案就像一个 File.Copy 命令一样简单。

标签: c# filestream read-write


【解决方案1】:

我找出了我的代码中的问题并修复了我的代码。

查看我的固定代码。看看我做了哪些改变。

    Public static byte[] data; // this variable will be store file's content.

    static void readfile(string path)
    {
        FileStream stm = File.Open(path, FileMode.Open); // open a file using filestream.
        int size = (int)stm.Length; // size of the file.
        data = new byte[size];

        while (size > 0) // loop until file' size is not 0.
        {
            int read = stm.read(data, totalbyte, size); // reading file's data.
            size -= read;
            totalbyte += read;
        }
    }
    static void writefile(string filename)
    {
        FileStream stm = File.Create(filename); // create a new file.
        byte[] bytes = data;

        Console.WriteLine("Writing data into file...");

        stm.Write(data, 0, data.Length); // writing data into created file.

        Console.WriteLine("data has been wrote into file.");
        stm.Close();
    }

【讨论】:

  • 这段代码过于复杂,可以简化为两行:static void ReadFile(string path) { data = File.ReadAllBytes(path); }static void WriteFile(string path) { File.WriteAllBytes(path, data); }。因此,实际上根本不需要这些方法,因为它们的作用与内置功能相同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-23
相关资源
最近更新 更多