C#基础笔记(第二十一天)

C#基础笔记(第二十一天)

大家好,又见面了,我是全栈君。

1.FIle类、Path类、Directory类复习
操作文件的
File 操作文件,静态类,对文件整体操作。拷贝、删除、剪切等。
Directory 操作目录(文件夹),静态类。
Path 对文件或目录的路径进行操作(很方便)[字符串]
Strean 文件流,抽象类。

Path 操作文件路径
File 操作文件
复制、剪切、创建、移除
//File.Create(@”C:\Users\SJD\Desktop\new.txt”);
//Console.WriteLine(“创建成功”);
//Console.ReadKey();

//File.Delete(@”C:\Users\SJD\Desktop\new.txt”);
//Console.WriteLine(“删除成功”);
//Console.ReadKey();

//File.Move(@”C:\Users\SJD\Desktop\new.txt”, @”C:\Users\SJD\Desktop\1.txt”);
//Console.WriteLine(“剪切成功”);
//Console.ReadKey();

使用File类来读取数据(读取小文件,因为是一次性读取,大文件用文件流来读取)

File的三个读取的方法

1. 以字节数组的形式读取
byte[] buffer = File.ReadAllBytes(@”C:\Users\SJD\Desktop\123.txt”);
string str = Encoding.Default.GetString(buffer, 0, buffer.Length);
Console.WriteLine(str);
Console.ReadKey();

2.以字符串数组的形式读取,用在操作每行数据上
string[]str= File.ReadAllLines(@”C:\Users\SJD\Desktop\123.txt”,Encoding.Default);
for (int i = 0; i < str.Length; i++)
{

Console.WriteLine(str[i]);
}
Console.ReadKey();

3.以字符串的形式读取,只用于看一下,不做任何操作
string str = File.ReadAllText(@”C:\Users\SJD\Desktop\123.txt”, Encoding.Default);
Console.WriteLine(str);
Console.ReadKey();

File的三个写入的方法

1. 以字节数组的形式写入,会覆盖原来的内容
string str = “今天天气好晴朗,处处好风光”;
byte[] buffer = Encoding.Default.GetBytes(str);
File.WriteAllBytes(@”C:\Users\SJD\Desktop\1.txt”, buffer);
Console.WriteLine(“写入成功”);
Console.ReadKey();

2.以字符串数组的形式写入一行一行
File.WriteAllLines(@”C:\Users\SJD\Desktop\1.txt”, new string[] { “今天天气好晴朗,处处好风光” });
Console.WriteLine(“写入成功”);
Console.ReadKey();

3.以字符串的形式直接写入
string str = “今天天气好晴朗,处处好风光”;
File.WriteAllText(@”C:\Users\SJD\Desktop\1.txt”, str);
Console.WriteLine(“写入成功”);
Console.ReadKey();

4.追加不覆盖,前面加上Append,有AppendAllLines和AppendAllText两种
File.AppendAllText(@”C:\Users\SJD\Desktop\1.txt”, “肯定没有覆盖”);
Console.WriteLine(“追加成功”);
Console.ReadKey();

Directory类
1.创建指定路径的文件夹
Directory.CreateDirectory(@”C:\Users\SJD\Desktop\新建文件夹”);
Console.WriteLine(“创建成功”);
Console.ReadKey();

2.删除指定路径的文件夹,文件夹目录不是空的就不能删,非要删除,后面加个true
Directory.Delete(@”C:\Users\SJD\Desktop\新建文件夹”,true);
Console.WriteLine(“删除成功”);
Console.ReadKey();

3.没有copy这个方法,但有move(剪切)
Directory.Move(@”C:\Users\SJD\Desktop\123″, @”C:\Users\SJD\Desktop\456″);
Console.WriteLine(“OK”);
Console.ReadKey();

4.Directory.GetFiles 获取你指定的文件夹下文件的全路径,后面加上*.格式,可以只读取选择的格式文件路径
string[] path = Directory.GetFiles(@”C:\Users\SJD\Desktop\456″, “*.jpg”);
for (int i = 0; i < path.Length; i++)
{

Console.WriteLine(path[i]);
}
Console.ReadKey();

2、文件流
两个大水缸,把一个缸中的水倒入另一个水缸。两种方式
.直接把一个缸中的水倒入另一个缸中。 file类
.用一个桶来把一个缸中的水舀到另一个缸中。 文件流
需要创建对象

FileStream 操作字节的
StreamReader StreamWriter 操作字符的
垃圾回收器不会帮我们自动回收占用的资源,必须要手动的close和dispose
但代码一多总忘记加这两个,所以我们把它写在using里面,让他自动的帮助我们释放

FileStream fsRead 读
using (FileStream fsRead=new FileStream(@”C:\Users\SJD\Desktop\123.txt”,FileMode.OpenOrCreate,FileAccess.Read))
{

byte[] buffer = new byte[1024 * 1024 * 5];
//表示本次读取实际读取到的有效字节数
int r= fsRead.Read(buffer, 0, buffer.Length);
string s= Encoding.Default.GetString(buffer, 0, r);
Console.WriteLine(s);
}
Console.ReadKey();

FileStream fsWrite 追加写入
using (FileStream fsWrite=new FileStream(@”C:\Users\SJD\Desktop\234.txt”,FileMode.OpenOrCreate,FileAccess.Write))
{

string str = “今天天气好晴朗”;
byte[] buffer = Encoding.Default.GetBytes(str);
fsWrite.Write(buffer, 0, buffer.Length);
Console.WriteLine(“写入成功”);
}
Console.ReadKey();

StreamReader
using (FileStream fsRead=new FileStream(@”C:\Users\SJD\Desktop\123.txt”,FileMode.OpenOrCreate,FileAccess.Read))
{

using (StreamReader sr = new StreamReader(fsRead,Encoding.Default))
{

while(!sr.EndOfStream)
{

Console.WriteLine(sr.ReadLine());
}
}
}
Console.ReadKey();

StreamWriter
byte[] buffer = new byte[1024 * 1024];
using (StreamWriter sw=new StreamWriter(@”C:\Users\SJD\Desktop\123.txt”,true,Encoding.Default,buffer.Length))
{

sw.WriteLine(“哈哈哈”);
}
Console.WriteLine(“OK”);
Console.ReadKey();

3、序列化
要将序列化对象的类 标记为可以被序列化
[Serializable]
把对象序列化成二进制

序列化
using (FileStream fsWrite = new FileStream(@”C:\Users\SJD\Desktop\123.txt”, FileMode.OpenOrCreate, FileAccess.Write))
{

BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fsWrite, p);
}
Console.WriteLine(“序列化成功”);
Console.ReadKey();

反序列化
Person p;
using (FileStream fsRead = new FileStrea
m(@”C:\Users\SJD\Desktop\123.txt”, FileMode.OpenOrCreate, FileAccess.Read))
{

BinaryFormatter bf = new BinaryFormatter();
p = (Person)bf.Deserialize(fsRead);
}
Console.WriteLine(p.Name);
Console.WriteLine(p.Age);
Console.WriteLine(p.Gender);
Console.ReadKey();

4、窗体应用程序

转载于:https://www.cnblogs.com/VSMinos/p/7873492.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107918.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 电驴资源站

    电驴资源站以下为电驴资源站或者电驴资源搜索站,按推荐度排名。1、http://www.simplecd.org/新兴站点,号称山寨版的Verycd,注册就可发资源帖,下载资源无铜光盘限制!曾经据说由于某臭名

    2022年7月1日
    44
  • 股票API

    实时股票数据接口大全股票数据的获取目前有如下两种方法可以获取:1.http/javascript接口取数据2.web-service接口1.http/javascript接口取数据1.1Sina股票数据接口以大秦铁路(股票代码:601006)为例,如果要获取它的最新行情,只需访问新浪的股票数据接口:http://hq.sinajs…

    2022年4月7日
    65
  • idea中添加tomcat_怎么查看Tomcat位置

    idea中添加tomcat_怎么查看Tomcat位置一、为IDEA添加Tomcat:添加全局的Tomcat:File–>Setting–>Build,Execution,Deployment–>ApplicationServers–>+–>TomcatServer–>选择要添加的服务器–>Ok为单个项目添加:AddConfiguration…[Run–>EditConfiguration…]–>+–>TomcatSer

    2022年10月18日
    6
  • html css制作404页面,CSS3绘制404页面

    html css制作404页面,CSS3绘制404页面标题有点噱了…最近在做一个交通有关的项目,想做一个类似标志牌的404,所以就有了这个.只用的CSS3中的旋转,效果如下上代码:Error.circle{width:200px;height:200px;border-radius:200px;border:15pxsolid#B22727;}.circle>div{color:#B22727;font:bol…

    2022年7月27日
    8
  • unbuntu安装google浏览器和谷歌浏览器驱动

    unbuntu安装google浏览器和谷歌浏览器驱动1、安装google浏览器sudowgethttp://www.linuxidc.com/files/repo/google-chrome.list-P/etc/apt/sources.list.d/wget-q-O-https://dl.google.com/linux/linux_signing_key.pub|sudoapt-keyadd-sudoapt-…

    2022年6月11日
    34
  • FireEye:2012年下半年高级威胁分析报告

    FireEye:2012年下半年高级威胁分析报告

    2021年8月22日
    55

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号