我们要两个代码,不知道从什么地方开始下手.因为我们这章学的是文件和流所以要用到这些东西
1,编程比较两个文件是否相同.
2.在窗体中放置一个编辑框空间,用来输入小雨255的数,放置2个按钮控件,标题分别为“输入下一个”和“存输入的数”。单机标题为:“村输入的数”按钮,把输入的所有字节类型数据存到文件中。单机标题“输入下一个”按钮,记录本次数日的书,晴空编辑框控件,准备输入下一个。在增加一个标题为“读文件”的按钮,单击该按钮,读如输入内容,在listbox控件显示。如果村委int类型或字符串,应如何实现?因为我们正在学这个,而且我对这些编程类问题超白吃,所以想大家来帮帮忙吧因为没有那么多的分数 所以不能给大家了哦
1,编程比较两个文件是否相同.
2.在窗体中放置一个编辑框空间,用来输入小雨255的数,放置2个按钮控件,标题分别为“输入下一个”和“存输入的数”。单机标题为:“村输入的数”按钮,把输入的所有字节类型数据存到文件中。单机标题“输入下一个”按钮,记录本次数日的书,晴空编辑框控件,准备输入下一个。在增加一个标题为“读文件”的按钮,单击该按钮,读如输入内容,在listbox控件显示。如果村委int类型或字符串,应如何实现?因为我们正在学这个,而且我对这些编程类问题超白吃,所以想大家来帮帮忙吧因为没有那么多的分数 所以不能给大家了哦
string ss = hc.ReadToEnd();
StreamReader hcg = new StreamReader(@"d:\2.txt");
string sss = hcg.ReadToEnd();
if (ss == sss)
{
MessageBox.Show("同");
}
else
{
MessageBox.Show("不同");
}
{
StreamReader hc = new StreamReader( @"c:\1.txt");
string ss = hc.ReadToEnd();
this.listBox1.Items.Add(ss);
} private void button1_Click(object sender, EventArgs e)
{
StreamWriter mm = new StreamWriter(@"c:\1.txt", false);
string sq = this.textBox1.Text;
mm.WriteLine(sq);
mm.Close();
MessageBox.Show("保存完成", "提示");
} private void button2_Click(object sender, EventArgs e)
{
this.textBox1.Clear();
StreamWriter mm = new StreamWriter(@"c:\1.txt", false);
string sq = this.textBox1.Text;
mm.WriteLine(sq);
mm.Close();
MessageBox.Show("保存完成", "提示"); }
StreamReader myStreamReader2 = new StreamReader(@"d:\b.txt");
string mystr1 = myStreamReader1.ReadToEnd();
string mystr2 = myStreamReader2.ReadToEnd();
if (mystr1.Equals(mystr2) == true)
Console.WriteLine("相等!");
else
Console.WriteLine("不相等!");
Console.ReadKey();
MD5 md5 = MD5.Create();
string str1, str2;
using(FileStream fs = new FileStream("C:\\test1.dat", FileMode.Open))
str1 = Encoding.Unicode.GetString(md5.ComputeHash(fs));
using(FileStream fs = new FileStream("C:\\test2.dat", FileMode.Open))
str1 = Encoding.Unicode.GetString(md5.ComputeHash(fs));
return str1 == str2;
MD5的典型应用是对一段信息(Message)产生信息摘要(Message-Digest),以防止被篡改。比如,在UNIX下有很多软件在下载的时候都有一个文件名相同,文件扩展名为.md5的文件,在这个文件中通常只有一行文本,大致结构如: MD5 (tanajiya.tar.gz) = 0ca175b9c0f726a831d895e269332461 这就是tanajiya.tar.gz文件的数字签名。MD5将整个文件当作一个大文本信息,通过其不可逆的字符串变换算法,产生了这个唯一的MD5信息摘要。为了让读者朋友对MD5的应用有个直观的认识,笔者以一个比方和一个实例来简要描述一下其工作过程:
大家都知道,地球上任何人都有自己独一无二的指纹,这常常成为公安机关鉴别罪犯身份最值得信赖的方法;与之类似,MD5就可以为任何文件(不管其大小、格式、数量)产生一个同样独一无二的“数字指纹”,如果任何人对文件做了任何改动,其MD5值也就是对应的“数字指纹”都会发生变化。
int bufSize = 2048000;
byte[] buf1 = new byte[bufSize];
byte[] buf2 = new byte[bufSize];
using (FileStream fs1 = new FileStream("C:\\Test1.data", FileMode.Open))
using (FileStream fs2 = new FileStream("C:\\Test1.data", FileMode.Open))
{
if (fs1.Length != fs2.Length) return false;
while (true)
{
int count = fs1.Read(buf1, 0, bufSize);
fs2.Read(buf2, 0, bufSize);
for (int i = 0; i < count; i++)
{
if (buf1[i] != buf2[i])
return false;
}
if (count < bufSize) break;
}
}
return true;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FileStream fs1 = new FileStream(@textBox3.Text, FileMode.Open, FileAccess.Read);
int a = 0;
string myS="", ch;
a = fs1.ReadByte();
while (a != -1)
{
ch = ((char)a).ToString();
myS += ch;
a = fs1.ReadByte();
}
textBox1.Text = myS;
fs1.Close();
} private void button2_Click(object sender, EventArgs e)
{ FileStream fs2 = new FileStream(@textBox4.Text, FileMode.Open, FileAccess.Read);
int a = 0;
string myS = "", ch;
a = fs2.ReadByte();
while (a != -1)
{
ch = ((char)a).ToString();
myS += ch;
a = fs2.ReadByte();
}
textBox2.Text = myS;
fs2.Close();
} private void button3_Click(object sender, EventArgs e)
{
FileStream fs1 = new FileStream(@textBox3.Text, FileMode.Open, FileAccess.Read);
FileStream fs2 = new FileStream(@textBox4.Text, FileMode.Open, FileAccess.Read);
byte[] array1 = new byte[fs1.Length];
byte[] array2 = new byte[fs2.Length];
fs1.Read(array1, 0, (int)fs1.Length);
fs2.Read(array2, 0, (int)fs2.Length);
if (array1.Length != array2.Length)
{
MessageBox.Show("not the same!!");
return;
}
for (int i = 0; i < array1.Length; i++)
{
if (array1[i] != array2[i])
{
MessageBox.Show("文件不相同!!");
return;
}
}
MessageBox.Show("文件相同!!");
} }
}
整数:
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication8
{
public partial class Form1 : Form
{
int []array=new int[100];
int i = 0;
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
try
{
int b = Convert.ToInt32(textBox1.Text);
array[i] = b;
i++;
textBox1.Text = "";
}
catch (FormatException)
{
MessageBox.Show("请输入整数!!");
textBox1.Text = "";
}
catch (OverflowException)
{
MessageBox.Show("数值超出整数范围!!");
textBox1.Text = "";
}
} private void button2_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(@"d:\1.txt", FileMode.Create, FileAccess.ReadWrite);
BinaryWriter w = new BinaryWriter(fs);
for (int j = 0; j < i;j++ )
w.Write(array[j]);
fs.Close();
} private void button3_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(@"d:\1.txt", FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
while (r.PeekChar() != -1)
{
string s = r.ReadInt32().ToString();
listBox1.Items.Add(s);
}
fs.Close();
}
}
}
string:
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication8
{
public partial class Form1 : Form
{
string []array=new string[100];
int i = 0;
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
string b = textBox1.Text;
array[i] = b;
i++;
textBox1.Text = "";
} private void button2_Click(object sender, EventArgs e)
{
StreamWriter w = new StreamWriter("d:\\2.txt", false);
for (int j = 0; j < i; j++)
w.WriteLine(array[j]);
w.Close();
} private void button3_Click(object sender, EventArgs e)
{
StreamReader r = new StreamReader("d:\\2.txt");
string s = "";
do
{
s = r.ReadLine();
if (s != null)
listBox1.Items.Add(s);
} while (s != null);
r.Close();
}
}
}
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication8
{
public partial class Form1 : Form
{
byte []array=new byte[100];
int i = 0;
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
try
{
byte a = Convert.ToByte(textBox1.Text);
array[i] = a;
i++;
textBox1.Text = "";
}
catch (FormatException)
{
MessageBox.Show("请输入数字!!");
textBox1.Text = "";
}
catch (OverflowException)
{
MessageBox.Show("输入的数不能超过255!!");
textBox1.Text = "";
} } private void button2_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(@"d:\1.dat", FileMode.Create, FileAccess.ReadWrite);
fs.Write(array, 0, i);
fs.Close();
} private void button3_Click(object sender, EventArgs e)
{
int a;
FileStream fs = new FileStream(@"d:\1.dat", FileMode.Open, FileAccess.Read);
a = fs.ReadByte();
while (a != -1)
{
string s = a.ToString();
listBox1.Items.Add(s);
a = fs.ReadByte();
}
fs.Close();
}
}
}所有的代码 供大家参考