现在有一个整数数组,比如1,3,5,10;如何把这4个字节写入到一个二进制文件呢?也就是这个文件不是文本,它应该也是4*4个字节=16字节的长度(如果系统的整数是4字节表示的话)。
同理,如何读取这样的一个文件呢,包括一次读指定的字节数
代码最好详细点
同理,如何读取这样的一个文件呢,包括一次读指定的字节数
代码最好详细点
解决方案 »
- 自定义控件问题
- 请帮忙看下这个程序有什么问题
- winfrom combobox能绑定多个tag值吗?我要每个combobox的value有个tab值
- 为什么多线程调用ExecuteNonQuery时会卡机?
- 关于textBox输入动态搜索?
- 急!!!急!!!小女子请教各位前辈怎么实现用hash表存放存储过程的参数列表,谢了!
- throw抛出异常又怎么样?应用程序还是中断了,throw到底怎么理解?
- 关于timer_tick的问题
- 自己搞了百度网盘搜索引擎,欢迎大家来吐糟
- 怎样保持在tabcontrol控件上,按下button按键画的画?
- 为什么还要long
- c#中为什么有了double类型还要有long????
//write
int[] intArray = { 1, 2, 3, 4 };
using(FileStream fs = new FileStream(@"C:\test.dat",FileMode.CreateNew))
using(BinaryWriter bw = new BinaryWriter(fs))
{
foreach (var val in intArray)
{
bw.Write(val);
}
}
//read
using (FileStream fs = new FileStream(@"C:\test.dat", FileMode.Open))
using (BinaryReader br = new BinaryReader(fs))
{
List<int> intList = new List<int>();
do
{
intList.Add(br.ReadInt32());
} while (fs.Position < fs.Length);
}
一次读指定的字节数:
br.ReadBytes(16);
FileStream fstr = new FileStream(fname, FileMode.OpenOrCreate, FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fstr);
foreach(int bwi in dt.AllGrids)
bw.Write(bwi);
bw.Write("\n");