C#对文本文件进行读写操作
[ 2007-09-18 14:00:41 | 作者: admin ]
p.s. 再简单的东西也要随时记下来做参考
读操作:写操作:Stream流读写文件:
二进制文件的读写:
读操作:
引用
using System;
using System.IO;
namespace ConsoleApplication2
{
/// <summary>
/// Summary description for Class2.
/// </summary>
public class Class1
{
private const string FILE_NAME="MyFile.txt";
public static void Main(String[] args)
{
if(!File.Exists(FILE_NAME)) //如果不文件存在,则抛出异常
{
Console.WriteLine("{0} does not exist!",FILE_NAME);
}
StreamReader sr=File.OpenText(FILE_NAME);
String input;
while((input=sr.ReadLine())!=null)
{
Console.WriteLine(input);
}
Console.WriteLine("The end of the Stream has been reched.");
sr.Close();
}
}
}
using System.IO;
namespace ConsoleApplication2
{
/// <summary>
/// Summary description for Class2.
/// </summary>
public class Class1
{
private const string FILE_NAME="MyFile.txt";
public static void Main(String[] args)
{
if(!File.Exists(FILE_NAME)) //如果不文件存在,则抛出异常
{
Console.WriteLine("{0} does not exist!",FILE_NAME);
}
StreamReader sr=File.OpenText(FILE_NAME);
String input;
while((input=sr.ReadLine())!=null)
{
Console.WriteLine(input);
}
Console.WriteLine("The end of the Stream has been reched.");
sr.Close();
}
}
}
引用
using System;
using System.IO;
namespace ConsoleApplication2
{
/// <summary>
/// Summary description for Class2.
/// </summary>
public class Class1
{
private const string FILE_NAME="MyFile.txt";
public static void Main(String[] args)
{
StreamWriter sr;
string report;
if(File.Exists(FILE_NAME)) //如果文件存在,则创建File.AppendText对象
{
sr=File.AppendText(FILE_NAME);
report="appended";
}
else //如果文件不存在,则创建File.CreateText对象
{
sr=File.CreateText(FILE_NAME);
report="created";
}
sr.WriteLine("This is my first file.");
Console.WriteLine("{0} {1}",FILE_NAME,report);
sr.Close();
}
}
}
using System.IO;
namespace ConsoleApplication2
{
/// <summary>
/// Summary description for Class2.
/// </summary>
public class Class1
{
private const string FILE_NAME="MyFile.txt";
public static void Main(String[] args)
{
StreamWriter sr;
string report;
if(File.Exists(FILE_NAME)) //如果文件存在,则创建File.AppendText对象
{
sr=File.AppendText(FILE_NAME);
report="appended";
}
else //如果文件不存在,则创建File.CreateText对象
{
sr=File.CreateText(FILE_NAME);
report="created";
}
sr.WriteLine("This is my first file.");
Console.WriteLine("{0} {1}",FILE_NAME,report);
sr.Close();
}
}
}
引用
private void button3_Click(object sender, EventArgs e)
{
//将textBox3.Text写入nihao.txt文件
Stream s = new FileStream("nihao.txt",FileMode.Create,FileAccess.Write);
//打开模式,访问方式
StreamWriter sw = new StreamWriter(s, Encoding.Default);//创建Stream流,指定编码方式
sw.Write(textBox3.Text);
sw.Close();
}
private void button2_Click(object sender, EventArgs e)
{
//将nihao.txt文件读出到textBox3.Text
Stream s = new FileStream("nihao.txt", FileMode.Open);
StreamReader sr = new StreamReader(s,Encoding.Default);
textBox3.Text= sr.ReadToEnd().ToString();
sr.Close();
s.Close();
//sr.BaseStream的Position或Seek()可移动文件流指针到的任意位置。
}
{
//将textBox3.Text写入nihao.txt文件
Stream s = new FileStream("nihao.txt",FileMode.Create,FileAccess.Write);
//打开模式,访问方式
StreamWriter sw = new StreamWriter(s, Encoding.Default);//创建Stream流,指定编码方式
sw.Write(textBox3.Text);
sw.Close();
}
private void button2_Click(object sender, EventArgs e)
{
//将nihao.txt文件读出到textBox3.Text
Stream s = new FileStream("nihao.txt", FileMode.Open);
StreamReader sr = new StreamReader(s,Encoding.Default);
textBox3.Text= sr.ReadToEnd().ToString();
sr.Close();
s.Close();
//sr.BaseStream的Position或Seek()可移动文件流指针到的任意位置。
}
二进制文件的读写:
引用
private void button5_Click(object sender, EventArgs e)
{
Stream s = new FileStream("nihao1.txt", FileMode.Create);
BinaryWriter sw = new BinaryWriter(s, Encoding.Unicode);
sw.Write(textBox3.Text);
sw.Close();
}
private void button6_Click(object sender, EventArgs e)
{
// MessageBox.Show(Convert.ToChar(97).ToString());
Stream s = new FileStream("nihao1.txt", FileMode.Open);
BinaryReader sr = new BinaryReader(s, Encoding.Unicode);
textBox3.Text=sr.ReadString().ToString();
sr.Close();
s.Close();
}
{
Stream s = new FileStream("nihao1.txt", FileMode.Create);
BinaryWriter sw = new BinaryWriter(s, Encoding.Unicode);
sw.Write(textBox3.Text);
sw.Close();
}
private void button6_Click(object sender, EventArgs e)
{
// MessageBox.Show(Convert.ToChar(97).ToString());
Stream s = new FileStream("nihao1.txt", FileMode.Open);
BinaryReader sr = new BinaryReader(s, Encoding.Unicode);
textBox3.Text=sr.ReadString().ToString();
sr.Close();
s.Close();
}
评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=993
这篇日志没有评论。
此日志不可发表评论。