按照从一个文件夹到另一个文件夹的名称递增顺序写入单个文件 [英] Write a single file in the increasing order of the names from one folder to another folder
问题描述
实际上我想从内存中读取一个文件并按照名称的增加顺序保存该文件,我只能写一个文件......
我尝试过:
actually i want read a file from memory and save that file in the increasing order of the names contineously ,i am able to write only one file so ......
What I have tried:
using (FileStream stream = File.OpenRead("C:\\veer1.raw"))
using (FileStream writeStream = File.OpenWrite("D:\\file2.raw"))
{
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);
}
推荐答案
您好会员13142768
,
您正在直接阅读和写作。您需要先将您在内存中读取的项目存储起来。为此,您可以使用通用列表
。以下行可能会更改:
HiMember 13142768
,
You are reading and writing straight-away. You need to store the items that you read in memory first. For this you can use a genericlist
. The following lines may change:
while ((bytesRead =
stream.Read(buffer, 0, 1024)) > 0)
{
writeStream.Write(buffer, 0, bytesRead);
}
收件人:
To:
List<byte[]> StrList = new List<byte[]>();
while ((bytesRead =
stream.Read(buffer, 0, 1024)) > 0)
{
StrList.Add(buffer);
}
也就是说,您正在将列表中的整个二进制内容读取为字节数组实体。但是我仍然不确定您的文件内容,因为您忘记回答。
排序后,您需要编写列表的内容。
内置的 sort
列表方法在这里不起作用,因为它有二进制字节数组。您将需要 IComparer
界面来协助。这是一个很好的解决方案:
c# - 排序字节列表或字节数组列表 - Stack Overflow [ ^ ]
That is, you are reading the entire binary contents in a list as byte array entities. But I am still not sure about the contents of your file as you forgot to answer that.
After sorting you need to write the contents of the list.
The built-in sort
method of list will not work here as it has binary byte array. You will need IComparer
interface to assist. Here is a good solution to such sorting:
c# - Sorting list of list of bytes or list of byte arrays - Stack Overflow[^]
这篇关于按照从一个文件夹到另一个文件夹的名称递增顺序写入单个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!