IO流的一般使用
string ce="测试";
File.WriteAllText(@"12.txt",ce,Encoding.UTF8);
//Path类
string s1 = @"C:\a";
string s2 = "b.txt";
//combine处理末尾\问题
string s3 = Path.Combine(s1, s2);
Console.WriteLine(s3);
string g1 = @"c:\windows\test\a.txt";
// 1.获取文件名
Console.WriteLine(Path.GetFileName(g1));
// 2.获取目录
Console.WriteLine(Path.GetDirectoryName(g1));
//3.获取文件名不包括扩展名
Console.WriteLine(Path.GetFileNameWithoutExtension(g1));
//4.获取扩展名
Console.WriteLine(Path.GetExtension(g1));
//5.获取完整路劲../获取父目录
Console.WriteLine(Path.GetFullPath("12.txt"));
//6.获取当前用户临时目录
Console.WriteLine(Path.GetTempPath());
//7.获取一个随机的文件名(也可以用作文件夹名)
Console.WriteLine(Path.GetRandomFileName());
//8.获取一个随机的文件名,并在临时目录下创建这个文件
Console.WriteLine(Path.GetTempFileName());
StreamWriter字符串生成js文件
string str = "hello";
string path = "D:/newfdc";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string FileName ="hello.js";
Encoding code = Encoding.GetEncoding("UTF-8");
StreamWriter sw = new StreamWriter(Path.Combine(path, FileName), false, code);
sw.Write(str);
sw.Flush();
sw.Close();
Console.WriteLine("OK");
Console.ReadKey();
file.copy使用
string str = "hello";
string path = "D:/newfdc";
if (!Directory.Exists(path)) {
Directory.CreateDirectory(path);
}
string FileName ="hello.js";
Encoding code = Encoding.GetEncoding("UTF-8");
string nowpath = Path.Combine(path, FileName);//现有文件
string path2 = "E:/new";
if (!Directory.Exists(path2)) {
Directory.CreateDirectory(path2);
}
string name = "copy.js";
File.Copy(nowpath,Path.Combine(path2,name),true);
Console.WriteLine("复制成功");
Console.ReadKey();
复制
if (true)
{
path = System.Configuration.ConfigurationSettings.AppSettings["jsupload"].ToString();
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
File.Copy(localfileName, Path.Combine(path, ftpfileName), true);
}
return true;
StreamWriter和reader
string mobile = "13628611111";
StreamWriter writer = new StreamWriter("SMS.txt", true, Encoding.UTF8);
writer.Write(mobile);
writer.Write(",");
writer.Flush();
writer.Close();
StreamReader reader = new StreamReader("SMS.txt");
string read= reader.ReadLine();
int i = 0;
int count = 0;
while (read.IndexOf(mobile, i) >= 0)
{
i=read.IndexOf(mobile,i)+1;
count++;
}
Console.WriteLine(count);
Console.ReadLine();
Serializable序列号案例
[Serializable]
public class ClassToSerialize
{
public int id = 100;
public string name = "Name";
[NonSerialized]
public string Sex = "男";
}
public void SerializeNow()
{
ClassToSerialize c = new ClassToSerialize();
FileStream fileStream = new FileStream("c:\\temp.dat", FileMode.Create);
BinaryFormatter b = new BinaryFormatter();
b.Serialize(fileStream, c);
fileStream.Close();
}
public void DeSerializeNow()
{
ClassToSerialize c = new ClassToSerialize();
c.Sex = "kkkk";
FileStream fileStream = new FileStream("c:\\temp.dat", FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryFormatter b = new BinaryFormatter();
c = b.Deserialize(fileStream) as ClassToSerialize;
Response.Write(c.name);
Response.Write(c.Sex);
fileStream.Close();
}
//////////////////////////////
[Serializable]
public class XRentManager
{
public XRentManager()
{
m_DicMyTraffics = new Dictionary<string, TrafficInfo>();
m_DicRenteds = new Dictionary<string, TrafficInfo>();//出租集合
}
///字典集合:保存可以出租的车
private Dictionary<string, TrafficInfo> m_DicMyTraffics;
internal Dictionary<string, TrafficInfo> DicMyTraffics
{
get { return m_DicMyTraffics; }
}
/// 获取可以出租的汽车集合
private Dictionary<string, TrafficInfo> m_DicRenteds;
/// <summary>
/// 汽车入库或者出租的车回库
/// </summary>
internal Dictionary<string, TrafficInfo> DicRenteds
{
get { return m_DicRenteds; }
}
}
FileStream fs = new FileStream("Students.bin", FileMode.OpenOrCreate);
BinaryFormatter bf = new BinaryFormatter();
if (fs.Length > 0)
{
m_RM = bf.Deserialize(fs) as XRentManager;//反序列化
}
bf = null;
fs.Close();
fs.Dispose();
//正序列化
XRentManager m_RM = new XRentManager();
FileStream fs = new FileStream("Students.bin", FileMode.OpenOrCreate);
BinaryFormatter bf = new BinaryFormatter();//创建序列化
bf.Serialize(fs, m_RM);//保存
bf = null;
fs.Close();
fs.Dispose();
Direcotry类(操作目录)
//1.创建一些目录
Directory.CreateDirectory("1");
//2.获取当前目录下的所有的直接子目录(文件夹)第二个参数包含"*I*",第三个参数,是当前目录还是所有搜索
string [] dirs= Directory.GetDirectories(@"c:\");
foreach(string item in dirs ){
Console.WriteLine(item);
}
//3.获取当前目录下的所有文件
string[] files = Directory.GetFiles(@"c:\");
foreach (string item in files) {
Console.WriteLine(item);
}
//4.判断是否有这个目录返bool
Directory.Exists(@"C:\test1");
//5.删目录
Directory.Delete(@"C:\test1",true);
//6.移动,剪切,移动到同一个地方,改名字,就是重名
Directory.Move(@"C:\a.txt", @"C:\a\a.txt");
File类
//1.判断文件是否存在?2.删除Delete
File.Exists(@"C:\1.txt");
//2.读取也有对应的写入
File.ReadAllLines();File.ReadAllBytes();File.ReadAllText();
文件流 FileStream
//1.创建一个文件流
FileStream fs = new FileStream(@"c:\txt.txt", FileMode.OpenOrCreate);
//2.读或写,每次读取的内容都有放在缓冲区中
byte[] byts=new byte[fs.Length]; //设置、字节
//第一个参数:文件中的字节读取到该数组中,0是从第0个索引开始放,第三个参数数最多读取的字长
fs.Read(byts, 0, byts.Length);
//文件流要关闭和释放
fs.Close(); fs.Dispose();