楼主看看下面的代码。。共同交流学习
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace FileStreamTest
{
class Program
{
static void Main(string[] args)
{
string path = "";
string content = "";
Console.WriteLine("请输入要保存的文件名,包括路径");
path = Console.ReadLine();
Console.WriteLine("请输入要保存的内容");
content = Console.ReadLine();
try
{
FileStream file = new FileStream(path, FileMode.Create);
byte[] bt = Encoding.UTF8.GetBytes(content);
file.Write(bt, 0, bt.Length);
//file.Flush();
}
catch (Exception ce)
{
Console.WriteLine("创建或写入文件时出错");
}
}
}
}
//读取文件
/*namespace FileStreamTest
{
class Program
{
static void Main(string[] args)
{
string path = "";
Console.WriteLine("请输入要读取文件的文件名,包括路径");
path = Console.ReadLine();
if(!File.Exists(path))
{
Console.WriteLine("文件不存在");
return;
}
try
{
FileStream file = new FileStream(path, FileMode.Open);
byte[] bt = new byte[file.Length];
file.Read(bt, 0, bt.Length);
string str = Encoding.Default.GetString(bt);
Console.WriteLine(str);
Console.ReadLine();
}
catch(Exception ce)
{
Console.WriteLine("读取文件出错");
}
}
}
}*/
/* //写入
string path = "d:/aa.txt";
string str2 = "abcde";
char[] str=new char[5];
str=str2.ToCharArray();
FileStream fs=new FileStream(path,FileMode.OpenOrCreate);
fs.Seek(0, SeekOrigin.End);
byte[] bt = Encoding.UTF8.GetBytes(str);
fs.Write(bt, 0, bt.Length);
//读取
string path = "d:/aa.txt";
FileStream fs=new FileStream(path,FileMode.OpenOrCreate);
byte[] bt =new byte[100];
fs.Read(bt, 0, bt.Length);
string str = Encoding.Default.GetString(bt);
Console.WriteLine(str);*/ |