1.理解文件和流概念。
2.熟悉文件操作的方法。
3.掌握Fileatream类使用方法。
4.掌握StreamReader和StreamWriter使用方法。
5.掌握BinaryReader和BinaryWriter使用方法。
【实验内容】
以下5道实验题目最终效果,大家可运行“可执行文件”目录中各题。
1.分别利用File类和FileInfo类、Directory类和DirectoryInfo类实现文件和目录的创建、删除、复制、移动等操作。
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 shiyan8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Text = "文件和目录操作实验";
}
private void Form1_Load(object sender, EventArgs e)
{
this.label1.Text = "分别利用File类和FileInfo类、Directory类和DirectoryInfo类实现文件和目录的创建、删除、复制、移动等操作";
this.label1.ForeColor = Color.Green;
this.button1.Text = "File类操作";
this.button2.Text = "FileInfo类操作";
this.button3.Text = "Directory类操作";
this.button4.Text = "DirectoryInfo类操作";
this.button5.Text = "结束";
}
private void button1_Click(object sender, EventArgs e)
{
StreamWriter sw = File.CreateText("c:\\abc.txt");
StreamReader sr = File.OpenText("c:\\abc.txt");
bool isFileExist = File.Exists("c:\\abc.txt");
File.Copy("c:\\abc.txt", "d:\\abc.txt", true);
File.Move("c:\\abc.txt", "d:\\abc.txt");
if (File.Exists("c:\\abc.txt"))
{
File.Delete("C:\\abc.txt");
}
}
private void button2_Click(object sender, EventArgs e)
{
FileInfo f = new FileInfo("c:\\abc.txt");
if (!f.Exists)
{
f.CreateText();
}
StreamWriter sw = f.AppendText();
sw.Write("增加一行数据");
sw.Dispose();
if (f.Exists)
{
f.CopyTo("d:\\abc.txt", true);
}
if (f.Exists)
{
f.Delete();
}
string name = f.Name;
long len = f.Length;
string text = f.Extension;
}
private void button3_Click(object sender, EventArgs e)
{
Directory.CreateDirectory("MyFolder1");
Directory.CreateDirectory("C:\\MyFolder2");
Directory.Move(@"E:\MyFolder", @"E:\Downloads\MyFolder");
//Directory.Move(@"E:\MyFolder", @"C:\MyFolder");
if (Directory.Exists(@"e:\MyFolder"))
{
Directory.Delete(@"e:\MyFolder");
}
DirectoryInfo myInfo = Directory.GetParent("e:\\download\\MyFolder");
string strParentinfo = myInfo.FullName;
}
private void button4_Click(object sender, EventArgs e)
{
DirectoryInfo myinfo = new DirectoryInfo("C:\\muFolder");
myinfo.Create();
string strmyFolderFullName = myinfo.FullName;
if (myinfo.Exists)
{
myinfo.Delete();
}
}
private void button5_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
2.FileStream表示文件流,它能够打开和关闭文件,并对文件进行单字节的读写操作。如图8-1所示,使用FileStream实现文件读写操作。
要求:文件的读操作之前,要进行判断文件是否存在。程序界面如图实验8-1所示效果。
图8-1
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace shiyan8
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.Text = "FileStream类";
}
private void Form2_Load(object sender, EventArgs e)
{
this.label1.Text = "输入文件名(含盘符路径):";
this.label2.Text = "文件内容如下:";
this.button1.Text = "读文件";
this.button2.Text = "写文件";
this.button3.Text = "结束";
}
private void button1_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(this.textBox1.Text, FileMode.Open, FileAccess.Read);
long i = fs.Length;
byte[] b = new byte[fs.Length];
fs.Read(b, 0, b.Length);
fs.Dispose();
UTF8Encoding tmp = new UTF8Encoding();
this.textBox2.Text = tmp.GetString(b);
}
private void button2_Click(object sender, EventArgs e)
{
string str = this.textBox1.Text;
FileStream fs = new FileStream(str, FileMode.Append, FileAccess.Write);
UTF8Encoding tmp = new UTF8Encoding();
byte[] b = tmp.GetBytes(this.textBox2.Text);
fs.Write(b, 0, b.Length);
fs.Dispose();
}
private void button3_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
3.StreamWrite类可以实现文本文件写操作。设计一个如图8-2所示的窗体。窗体上有两个命令按钮、一个文本框。
具体要求:第一次单击“保存”按钮时,弹出一个保存对话框,并将内容利用StreamWrite类保存到指定文件中;以后再单击“保存”按钮,自动将最新内容保存下来,不再弹出对话框。
using System;
using System.IO;
using System.Windows.Forms;
namespace shiyan8
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
this.Text = "StreamWrite类";
}
private void Form3_Load(object sender, EventArgs e)
{
this.button1.Text = "保存";
this.button2.Text = "结束";
}
private void button1_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = "d:\\";
saveFileDialog1.Filter = "*.txt|*.txt";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
DialogResult dr = saveFileDialog1.ShowDialog();
if (dr == DialogResult.OK && saveFileDialog1.FileName.Length > 0)
{
FileStream fs1 = new FileStream(saveFileDialog1.FileName, FileMode.Create);
StreamWriter sw = new StreamWriter(fs1);
sw.Write(textBox1.Text);
sw.Close();
fs1.Close();
}
DialogResult btn1 = MessageBox.Show("保存文件成功", "", MessageBoxButtons.OK);
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
4.复制图片。创建一个如图实验8-3所示的窗体,窗体上放置两个按钮(button1、button2)、一个标签(label1,用于显示所选图片路径和文件名)、一个图片框(pictureBox1,用于显示所选图片)、一个打开对话框(penFileDialog1)、一个保存对话框(saveFileDialog1)。
实现功能:
(1)程序运行时,用户单击“选择图片”按钮,即可打开一个“打开对话框”,在该对话框中,文件类型可以是*.jpg或*.gif ;
(2)用户选取某个图片后,该图片显示在pictureBox1中,显示方式为缩放图片适应pictureBox1。此外,该图片文件路径及完整文件名在标签label1中显示出来。
(3)用户单击“复制图片”按钮,即可打开一个“保存对话框”。在该对话框中,文件扩展名类型可以是*.jpg或*.gif;用户在该对话框中输入保存后文件名即可实现复制图片框pictureBox1中的图片。
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 shiyan8
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
this.Text = "复制图片";
}
private void Form4_Load(object sender, EventArgs e)
{
this.button1.Text = "选择图片";
this.button2.Text = "复制图片";
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog saveFileDialog1 = new OpenFileDialog();
saveFileDialog1.InitialDirectory = "d:\\";
saveFileDialog1.Filter = "*.jpg|*.jpg|*.gif|*.gif";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
DialogResult dr = saveFileDialog1.ShowDialog();
if (dr == DialogResult.OK && saveFileDialog1.FileName.Length > 0)
{
string strpath = saveFileDialog1.FileName;
this.label1.Text = strpath;
FileStream fs = new FileStream(strpath, FileMode.Open);
byte[] by = new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
by = br.ReadBytes(Convert.ToInt32(fs.Length));
MemoryStream ms = new MemoryStream(by);
Bitmap btm = new Bitmap(ms);
pictureBox1.Image = btm;
br.Close();
ms.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = "d:\\";
saveFileDialog1.Filter = "*.jpg|*.jpg|*.gif|*.gif";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
DialogResult dr = saveFileDialog1.ShowDialog();
if (dr == DialogResult.OK && saveFileDialog1.FileName.Length > 0)
{
string str = saveFileDialog1.FileName;
FileStream ip = new FileStream(this.label1.Text, FileMode.Open, FileAccess.Read);
FileStream op = new FileStream(str, FileMode.OpenOrCreate, FileAccess.Write);
int buffer = Convert.ToInt32(ip.Length);
int bytes;
byte[] by = new byte[buffer];
BinaryReader br = new BinaryReader(ip);
BinaryWriter bw = new BinaryWriter(op);
while ((bytes = br.Read(by, 0, buffer))>0)
{
bw.Write(by, 0, bytes);
}
br.Close();
bw.Close();
ip.Close();
op.Close();
}
}
}
}
5.递归挂树。创建一个如图实验8-4所示的窗体,窗体上放置一个按钮(名称为button1,文本为“递归挂树”)、TreeView控件(treeView1)。单击“递归挂树”按钮,实现效果如图8-4所示。递归挂树关键代码为Class1中的Open方法和GetTreeNode,如图8-5所示。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace shiyan8
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
this.Text = "递归挂树窗体";
}
private void Form5_Load(object sender, EventArgs e)
{
this.button1.Text = "递归挂树";
this.button2.Text = "结束";
}
private void button1_Click(object sender, EventArgs e)
{
Class1 class1 = new Class1();
string xmlfilepath = "C:\\Users\\苗华\\source\\repos\\shiyan8\\shiyan8\\XMLFile1.xml";
class1.Open(treeView1, xmlfilepath);
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Class1文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace shiyan8
{
class Class1
{
public void Open(TreeView tv,String xmlFileName)
{
try
{
tv.Nodes.Clear();
XmlDocument xml = new XmlDocument();
xml.Load(xmlFileName);
XmlNode root = xml.DocumentElement;
TreeNode tn = new TreeNode();
tn.Text = root.Name;
tv.Nodes.Add(tn);
GetTreeNode(root,tn);
}
catch(Exception ex)
{
throw ex;
}
}
private void GetTreeNode(XmlNode xn,TreeNode tn)
{
foreach(XmlNode item in xn.ChildNodes)
{
TreeNode temp = null;
if (item.HasChildNodes)
{
temp = new TreeNode(item.Name);
}
else
{
temp = new TreeNode(item.InnerText);
}
tn.Nodes.Add(temp);
GetTreeNode(item, temp);
}
}
}
}