将数据分批保存到不同文件夹下

来源:互联网 发布:圆周率公式算法 编辑:程序博客网 时间:2024/06/07 09:53
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;namespace test{ class Program {   static void Main(string[] args)   {    for (int count = 0; count < 20; count++)    {     string countstr= count.ToString();     string filename = countstr + ".txt";     int folderNum = count / 10;     char[] attr = { 'A', 'B', 'C', 'D', 'E' };     string savepath = "D:\\"+ folderNum.ToString();//每保存10条数据则新建一个文件夹     if (Directory.Exists(savepath))     {        ;     }     else    {     Directory.CreateDirectory(savepath);//创建文件夹    }    FileStream myStream = new FileStream(savepath + "\\" + filename, FileMode.OpenOrCreate, FileAccess.ReadWrite); //创建文件    BinaryWriter myWriter = new BinaryWriter(myStream);    myWriter.Write(attr, 0, attr.Length);//往创建好的txt中写入数据    myWriter.Close();    myStream.Close();   }  } }}

运行程序,结果如下:
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

原创粉丝点击