binaryreader java,c# 二进制文件的读写 | 菜鸟教程 -ag凯发k8国际
using system;
using system.io;
namespace binaryfileapplication
{
class program
{
static void main(string[] args)
{
binarywriter bw;
binaryreader br;
int i = 25;
double d = 3.14159;
bool b = true;
string s = "i am happy";
#region
//尝试实例化,创建二进制文件
try
{
bw = new binarywriter(new filestream("mydata", filemode.create));
}
catch (ioexception e)
{
console.writeline(e.message "\n cannot create file.");
return;
}
#endregion
try//尝试写入二进制文件
{
bw.write(i);
bw.write(d);
bw.write(b);
bw.write(s);//写入四行,每次写入一行
}
catch (ioexception e)
{
console.writeline(e.message "\ncannot write to file.");
return;
}
bw.close();//执行完写入程序后关闭该二进制文件
try//尝试打开该二进制文件
{
br = new binaryreader(new filestream("mydata", filemode.open));
}
catch (ioexception e)
{
console.writeline(e.message "cannot open file.");
return;
}
try//尝试读取该二进制文件
{
//按顺序读取数据,这里的读取方式对应了之前的存储方式,并且按顺序操作
i = br.readint32();
console.writeline("integer data: {0}", i);
d = br.readdouble();
console.writeline("double data: {0}", d);
b = br.readboolean();
console.writeline("boolean data: {0}", b);
s = br.readstring();
console.writeline("string data: {0}", s);
}
catch (ioexception e)
{
console.writeline(e.message "\n cannot read file.");
return;
}
br.close();//读取完后关闭该二进制文件
console.readkey();
}
}
}
奇幻风之旅
奇幻风之旅
162***8796@qq.com7个月前 (08-31)
总结
以上是ag凯发k8国际为你收集整理的binaryreader java,c# 二进制文件的读写 | 菜鸟教程的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 用matlab定义2020年元旦是星期三
- 下一篇: