using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace BinaryIO
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string fileName = "MyNew.data";//定义字符串变量存储文件名字符串
if (File.Exists(fileName))//判断文件是否存在
{
MessageBox.Show("当前文件已经存在");
}
else
{
FileStream fs = new FileStream(fileName, FileMode.Create);
//使用FileStream类创建文件
BinaryWriter writer = new BinaryWriter(fs);//将BinaryWriter类实例化
writer.Write(textBox1.Text);
//调用BinaryWriter类的Write()方法将文本框中的数据写入
for (int i = 0; i < 300; i++)
{
writer.Write(10 + i);
}
MessageBox.Show("写入文件成功");
textBox1.Text = "";//关闭BinaryWriter流
fs.Close();//关闭FileStream流
}
}
private void button2_Click(object sender, EventArgs e)
{
string fileName = "MyNew.data";//定义字符串变量存储文件名字符串
if (!(File.Exists(fileName)))//判断要读取的文件是否存在
{
MessageBox.Show("当前文件不存在");
return;
}
string strData = "";
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//以读取已有文件的方式创建FileStream的实例对象
BinaryReader reader = new BinaryReader(fs);//实例化BinaryReader类
strData = reader.ReadString();//调用BinaryReader的ReadString方法
for (int i = 0; i < 300; i++)//循环读取文件内容
{
if (i == 0)
{
strData += reader.ReadInt32().ToString();
//每次读取4个字节带符号的整数值,并转换为字符串类型
}
else
{
strData += " || " + reader.ReadInt32().ToString();
}
}
textBox2.Text = strData;//读取的文件内容显示在textBox2中
fs.Close();//关闭文件流对象
reader.Close();//关闭二进制文件读对象存储文件名字符串
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string fileName = "MyNew.data";//定义字符串变量存储文件名字符串
if (File.Exists(fileName))//判断文件是否存在
{
MessageBox.Show("当前文件已经存在");
}
else
{
FileStream fs = new FileStream(fileName, FileMode.Create);
//使用FileStream类创建文件
BinaryWriter writer = new BinaryWriter(fs);//将BinaryWriter类实例化
writer.Write(textBox1.Text);
//调用BinaryWriter类的Write()方法将文本框中的数据写入
for (int i = 0; i < 300; i++)
{
writer.Write(10 + i);
}
MessageBox.Show("写入文件成功");
textBox1.Text = "";//关闭BinaryWriter流
fs.Close();//关闭FileStream流
}
}
private void button2_Click(object sender, EventArgs e)
{
string fileName = "MyNew.data";//定义字符串变量存储文件名字符串
if (!(File.Exists(fileName)))//判断要读取的文件是否存在
{
MessageBox.Show("当前文件不存在");
return;
}
string strData = "";
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//以读取已有文件的方式创建FileStream的实例对象
BinaryReader reader = new BinaryReader(fs);//实例化BinaryReader类
strData = reader.ReadString();//调用BinaryReader的ReadString方法
for (int i = 0; i < 300; i++)//循环读取文件内容
{
if (i == 0)
{
strData += reader.ReadInt32().ToString();
//每次读取4个字节带符号的整数值,并转换为字符串类型
}
else
{
strData += " || " + reader.ReadInt32().ToString();
}
}
textBox2.Text = strData;//读取的文件内容显示在textBox2中
fs.Close();//关闭文件流对象
reader.Close();//关闭二进制文件读对象存储文件名字符串
}
}
}
测试成功。
“using System.Threading.Tasks;”vs2008用不了这个。。
我只是贴我完整的代码啦。没用到的。
writer.Write(textBox1.Text);
//调用BinaryWriter类的Write()方法将文本框中的数据写入
for (int i = 0; i < 300; i++)
{
writer.Write(10 + i);
}