你好
我一直致力于类似终端的应用程序,以便更好地使用c#编程,这只是帮助我学习的东西。我决定添加一个功能,将文件完全按原样复制到一个新文件......它似乎工作得非常完美。在Notepad ++中打开时,文件的长度只有几行,而且非常非常接近实际文件大小。但是,文件的重复副本永远不会运行。它说文件已损坏。我有一种感觉,它是在读取和重写二进制文件的方法中,我创建的文件。代码如下,谢谢你的帮助。对于意大利面条代码也很抱歉,当我搞砸新想法时,我有点草率。
处理文件复制/写入的类
using System;
using System.IO;
//using System.Collections.Generic;
namespace ConsoleFileExplorer
{
class FileTransfer
{
private BinaryWriter writer;
private BinaryReader reader;
private FileStream fsc; // file to be duplicated
private FileStream fsn; // new location of file
int[] fileData;
private string _file;
public FileTransfer(String file)
{
_file = file;
fsc = new FileStream(file, FileMode.Open);
reader = new BinaryReader(fsc);
}
// Reads all the original files data to an array of bytes
public byte[] ReadAllDataToArray()
{
byte[] bytes = reader.ReadBytes((int)fsc.Length); // reading bytes from the original file
return bytes;
}
// writes the array of original byte data to a new file
public void WriteDataFromArray(byte[] fileData, string path) // got a feeling this is the problem :p
{
fsn = new FileStream(path, FileMode.Create);
writer = new BinaryWriter(fsn);
int i = 0;
while(i < fileData.Length)
{
writer.Write(fileData[i]);
i++;
}
}
}
}
与此类互动的代码。
(睡眠(5000)是因为我在第一次尝试时期待错误...
case '3':
Console.Write("Enter source file: ");
string sourceFile = Console.ReadLine();
if (sourceFile == "")
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.Error.WriteLine("Must input a proper file path.\n");
Console.ForegroundColor = ConsoleColor.White;
Menu();
} else {
Console.WriteLine("Copying Data"); System.Threading.Thread.Sleep(5000);
FileTransfer trans = new FileTransfer(sourceFile);
//copying the original files data
byte[] data = trans.ReadAllDataToArray();
Console.Write("Enter Location to store data: ");
string newPath = Console.ReadLine();
// Just for me to make sure it doesnt exit if i forget
if(newPath == "")
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.Error.WriteLine("Cannot have empty path.");
Console.ForegroundColor = ConsoleColor.White;
Menu();
} else
{
Console.WriteLine("Writing data to file"); System.Threading.Thread.Sleep(5000);
trans.WriteDataFromArray(data, newPath);
Console.WriteLine("File stored.");
Console.ReadLine();
Console.Clear();
Menu();
}
}
break;
文件与新文件比较 右键单击 - &gt;在新标签中打开可能是一个好主意
答案 0 :(得分:0)
C#中有一个File.Copy()方法,你可以在这里看到https://msdn.microsoft.com/ru-ru/library/c6cfw35a(v=vs.110).aspx
如果您想自己实现它,请尝试在方法中放置一个断点并使用调试。这就像一个关于渔民和上帝的故事,他们给渔民一根竿子 - 得到一条鱼,而不是鱼。
另外,在最后一个方法中查看int [] fileData和byte [] fileData,也许这就是问题。
答案 1 :(得分:0)
您没有正确处理文件流和二进制编写器。两者都倾向于缓冲数据(这是一件好事,特别是当你一次写一个字节时)。使用using
,您的问题就会消失。当然,除非有人在你正在阅读时编辑文件。
BinaryReader
和BinaryWriter
不只是写“原始数据”。它们还根据需要添加元数据 - 它们被设计用于序列化和反序列化,而不是读取和写入字节。现在,在特别使用ReadBytes
和Write(byte[])
的特定情况下,这些实际上只是原始字节;但是仅仅为此使用这些类并没有多大意义。读取和写入字节是每个Stream
给你的 事物 - 包括FileStream
s。这里没有理由使用BinaryReader
/ BinaryWriter
- 文件流可以为您提供所需的一切。
更好的方法是简单地使用
using (var fsn = ...)
{
fsn.Write(fileData, 0, fileData.Length);
}
甚至只是
File.WriteAllBytes(fileName, fileData);
也许你认为一次写一个字节更接近“金属”,但事实并非如此。在此期间,CPU不会一次将一个字节传递给硬盘驱动器。相反,硬盘驱动器直接从RAM复制数据 ,而无需CPU干预。而且大多数硬盘驱动器仍然无法从物理介质中写入(或读取)任意数量的数据 - 相反,您正在读写整个扇区。如果系统确实一次写入一个字节,你只需要反复重写相同的扇区,再写一个字节。
更好的方法是使用以下事实:打开文件流,将文件从源传输到目标,而不是先将所有内容读入内存,然后将其写回磁盘。