FileStream类操作文件要先实例化一个对象,有不同形式的重载,其中一个最重要的FileMode,说明如下
Appednd | 打开现有文件并查找到文件尾(就是第二次打开这个文件重写的话是在第一次的后面往后加),或创建新文件,它只能与FileAccess.Write一起用 |
---|---|
Create | 指定操作系统创建新文件,如果文件存在,则被改写;如果文件不存在,则使用CreateNew; |
CreateNew | 指定操作系统创建新文件,如果存在引发IOException异常 |
Open | 指定操作系统打开现有文件,打开后的能力取决于FileMode,如果该文件不存在引发System.IO.FileNotFoundException |
OpenOrCreate | 指定操作系统打开文件(如果存在),否则创建新文件。 |
文本文件的写入和读取
文本文件的写入与读取主要是通过StreamWriter类和StreamReader类来实现。默认使用UTE8Encoding编码。
StreamWriter类的常用方法
Close | 关闭当前的StreamWriter和基础流 |
---|---|
Write | 写入到StreamWriter的此实例中 |
WriteLine | 写入重载参数指定的某些数据,后跟行结束符 |
StreamReader是专门用于读取文本文件的类,
Close | 关闭当前的StreamReader |
---|---|
Read | 读取输入字符串中的下一个字符或下一组字符 |
ReadBlock | 从当前流中读取最大count的字符并从index开始将该数据写入到Buffer |
ReadLine | 从基础字符串中读取一行 |
ReadToEnd | 将整个流或从流的当前位置到流的结尾作为字符串读取 |
在WPF中
private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog1 = new System.Windows.Forms.OpenFileDialog();//要添加using System.Windows.Forms;
// 文件格式必须是csv,不能是xls不然乱码,CSV是文本文件,用记事本就能打开,XLS是二进制的文件只有用EXCEL才能打
try
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string fullPath1 = openFileDialog1.FileName;//选择要读取的文件路径
getColumnDB2(fullPath1);
}
}
catch { }
}
public void getColumnDB2(string ExcelName)
{
try
{
FileStream fs = new FileStream(ExcelName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8);
FileStream fs2 = new FileStream(@"C:\Users\yxz\Desktop\欢迎3.txt", FileMode.Append,FileAccess.Write);//注意这样写法,"欢迎.txt"文件必须存在
StreamWriter sw = new StreamWriter(fs2);
while (sr.Peek() > 0)
{
string str = sr.ReadLine();
string[] strSplit = str.Split(',');//分割字符串
double[] val = new double[strSplit.GetLength(0)];//strSplit.GetLength(0)表示 str这行的有多少元素,下标从零开始
if (double.TryParse(strSplit[6], out val[6]))//TryParse将数字的字符串表示形式转换为他的等效双精度浮点数
{ x_Number.Add(val[6]); sw.Write(val[6] + " , "); } //x轴
if (double.TryParse(strSplit[0], out val[0]))//TryParse将数字的字符串表示形式转换为他的等效双精度浮点数
{ y_Number.Add(val[0]); sw.WriteLine(val[0]); } //y轴
}
fs.Close();
sr.Close();
sw.Close();
fs2.Close();
}
catch(IOException ex) { System.Windows.Forms.MessageBox.Show(ex.Message); }
}
在Winform中
实例:在winform中要添加saveFileDialog控件和openFileDialog控件
private void Write_Click(object sender, EventArgs e)
{
if(textBox7.Text==string.Empty)
{
MessageBox.Show("写入的文件内容不能为空!");
}
else
{
saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
//使用”另存为“对话框中输入的文件名实例化StreamWriter对象
StreamWriter sw = new StreamWriter(saveFileDialog1.FileName, true);
sw.WriteLine(textBox7.Text);//向创建的文件中写入内容
sw.Close();
textBox7.Text = string.Empty;
}
}
}
private void read_Click(object sender, EventArgs e)
{
openFileDialog2.Filter = "文本文件(*.txt)|*.txt";
if (openFileDialog2.ShowDialog() == DialogResult.OK)
{
textBox7.Text = string.Empty;
//使用”打开“对话框中选择的文件实例化StreamReader对象
StreamReader sr = new StreamReader(openFileDialog2.FileName);
textBox7.Text = sr.ReadToEnd();
sr.Close();
}
}
也可以直接从文件中创建StreamWriter对象(建议不要采用这种方法):
StreamWriter sw = new StreamWriter(“test.txt”,true);此构造函数中有两个参数,一个是文件名,另一个是布尔值,这个布尔值规定创建对象的方式如下:
如果此值为false,则创建一个新文件,如果存在原文件,则覆盖。
如果此值为true,则打开文件保留原来数据,如果找不到文件,则创建新文件。
与 创建FileStream对象不同,创建StreamWriter对象不会提供一组类似的选项:除了使用Boolean值添加到文件的末尾或创建新文件之 外,根本没有像FileStream类那样指定FileMode属性的选项。而且,没有设置FileAccess属性的选项,因此总是有对文件的读/写权 限。为了使用高级参数,必须先在FileStream构造函数中指定这些参数,然后在FileStream对象中创建StreamWriter。
如下:
StreamWriter sw2 = new StreamWriter(@"C:\Users\yxz\Desktop\欢迎4.txt",false);//这样直接指定路径
sw2.write("123");
二进制文件的写入和读取
二进制文件的写入和读取主要通过BinaryWriter类和BinaryReader类来实现,
1.BinaryWriter类
Close | 关闭当前的 BinaryWriter和基础流 |
---|---|
Seek | 设置当前流中的位置 |
Write | 将值写入当前流 |
2.BinaryReader类
Close | 关闭当前阅读器和基础流 |
---|---|
peekChar | 返回下一个可用的字符,并且不提升字节或字符的位置 |
Read | 从基础流中读取字符,并提升流的当前位置 |
ReadBoolean | – |
ReadByte | 从当前流中读取下一个字节,并使流的当前位置提升一个字节 |
ReadBytes | 从当前流中将count个字节读入字节数组,并使当前位置提升count个字节 |
ReadChar | 从当前流中读取下一个字符,并根据所使用的Encoding和从流中读取的特定字符,提升流的当前位置 |
ReadChars | 从当前流中读取count个字符,以字符数组的形式返回数据,并根据所使用的Encoding和从流中读取的特定字符,提升当前位置 |
ReadInt32 | 从当前流中读取4个字节有符号整数,并使流的当前位置提升4个字节 |
ReadString | 从当前流中读取一个字符串。字符串有长度前缀,一次将7位编码为整数 |
实例
private void Write_Click(object sender, EventArgs e)
{
if(textBox7.Text==string.Empty)
{
MessageBox.Show("写入的文件内容不能为空!");
}
else
{
saveFileDialog1.Filter = "二进制文件(*.dat)|*.dat";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
//使用”另存为“对话框中输入的文件名实例化StreamWriter对象
FileStream myStream = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
BinaryWriter myWriter = new BinaryWriter(myStream);
myWriter.Write(textBox7.Text);
myWriter.Close(); //关闭当前二进制写入流
myStream.Close(); //关闭当前文件流
textBox7.Text = string.Empty;
}
}
}
private void read_Click(object sender, EventArgs e)
{
openFileDialog2.Filter = "二进制文件(*.dat)|*.dat";
if (openFileDialog2.ShowDialog() == DialogResult.OK)
{
textBox7.Text = string.Empty;
FileStream mystream = new FileStream(openFileDialog2.FileName, FileMode.Open, FileAccess.Read);
BinaryReader myReader = new BinaryReader(mystream);
if (myReader.PeekChar() != -1)
{
textBox7.Text = myReader.ToString();
}
mystream.Close();
myReader.Close();
}
}