c# 以二进制读取文本文件
时间:2023-02-28 15:23:26
using System;
using System.IO;
public class FileApp
{
public static void Main()
{
// 在当前目录创建一个文件myfile.txt,对该文件具有读写权限
FileStream fsMyfile = new FileStream("myfile.txt" , FileMode.Create, FileAccess.ReadWrite);
// 创建一个数据流写入器,和打开的文件关联
StreamWriter swMyfile = new StreamWriter(fsMyfile);
// 以文本方式写一个文件
swMyfile.WriteLine("Hello, World");
swMyfile.WriteLine("abcdefghijklmnopqrstuvwxyz");
swMyfile.WriteLine("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
swMyfile.WriteLine("0123456789");
// 冲刷数据(把数据真正写到文件中去)
// 注释该句试试看,程序将报错
swMyfile.Flush();
// 以文本方式读文件
// 创建一个数据流读入器,和打开的文件关联
StreamReader srMyfile= new StreamReader(fsMyfile);
// 把文件指针重新定位到文件的开始
srMyfile.BaseStream.Seek(0, SeekOrigin.Begin);
// 打印提示信息
Console.WriteLine("****************以文本方式读文件*********************");
// 打印文件文本内容
string s1;
while((s1 = srMyfile.ReadLine())!=null)
{
Console.WriteLine(s1);
}
Console.WriteLine();
// 以文本方式读文件结束
// 以二进制方式读文件
// 创建一个二进制数据流读入器,和打开的文件关联
BinaryReader brMyfile= new BinaryReader (fsMyfile);
// 把文件指针重新定位到文件的开始
brMyfile.BaseStream.Seek(0, SeekOrigin.Begin);
// 打印提示信息
Console.WriteLine("****************以二进制方式读文件*********************");
// 打印文件文本内容
Byte b1;
while(brMyfile.PeekChar()>-1)
{
b1=brMyfile.ReadByte();
// 13为"\n",表示回车;10为"\r",表示换行
if(b1 != 13 && b1 != 10)
{
Console.Write("{0}",b1.ToString());
Console.Write(".");
}
else
{
Console.WriteLine();
}
}
Console.WriteLine("\n");
// 以二进制方式读文件结束
// 关闭以上new的各个对象
brMyfile.Close();
srMyfile.Close();
fsMyfile.Close();
// 读取文件属性
// 打印提示信息
Console.WriteLine("****************读取文件属性*********************");
FileInfo fiMyfile=new FileInfo("myfile.txt");
Console.WriteLine("文件名 : {0}",fiMyfile.Name);
Console.WriteLine("文件名(含路径) : {0}",fiMyfile.FullName);
Console.WriteLine("文件大小(bytes) : {0}",fiMyfile.Length);
Console.WriteLine("文件创建时间 : {0}",fiMyfile.CreationTime);
}
}
标签:c#,二进制,文本文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅析Spring Boot中的spring-boot-load模块
2023-11-23 02:39:31
![](https://img.aspxhome.com/file/2023/6/58756_0s.png)
Java文件操作之IO流 File类的使用详解
2023-07-26 00:49:41
java按指定编码写入和读取文件内容的类分享
2023-06-18 10:13:01
SpringBoot+EasyPoi实现excel导出功能
2022-05-08 06:47:35
![](https://img.aspxhome.com/file/2023/4/99744_0s.jpg)
C#编写游戏客户端的实现代码
2021-08-28 06:24:58
![](https://img.aspxhome.com/file/2023/9/80109_0s.png)
Spring Boot接口限流的常用算法及特点
2023-12-20 05:16:30
![](https://img.aspxhome.com/file/2023/8/80308_0s.jpg)
java对象序列化与反序列化的默认格式和json格式使用示例
2021-12-07 18:22:23
解决SpringBoot运行Test时报错:SpringBoot Unable to find
2021-11-15 16:48:56
![](https://img.aspxhome.com/file/2023/9/79899_0s.png)
jdk1.8中的for循环问题记录
2023-10-16 04:08:34
![](https://img.aspxhome.com/file/2023/4/82044_0s.png)
springboot中的springSession的存储和获取实现
2023-11-04 10:23:14
mybatis中映射文件(mapper)中的使用规则
2021-06-28 20:09:08
![](https://img.aspxhome.com/file/2023/8/76248_0s.png)
spring boot使用thymeleaf跳转页面实例代码
2021-09-14 09:46:16
![](https://img.aspxhome.com/file/2023/1/122261_0s.png)
一篇文章带你使用C语言编写内核
2022-01-12 12:57:48
![](https://img.aspxhome.com/file/2023/4/109764_0s.jpg)
Android的HTTP类库Volley入门学习教程
2022-06-08 11:56:51
![](https://img.aspxhome.com/file/2023/3/139483_0s.png)
SpringMVC请求流程源码解析
2021-08-07 03:35:11
![](https://img.aspxhome.com/file/2023/3/64533_0s.jpg)
SpringBoot利用@Retryable注解实现接口重试
2023-03-08 19:54:42
C#中数组、ArrayList、List、Dictionary的用法与区别浅析(存取数据)
2021-05-27 03:55:49
![](https://img.aspxhome.com/file/2023/0/104340_0s.png)
springboot整合nacos,如何读取nacos配置文件
2022-12-30 19:55:25
![](https://img.aspxhome.com/file/2023/0/80260_0s.png)
SpringBoot实现接口等幂次校验的示例代码
2022-01-21 10:49:00
Android开发中ImageLoder加载网络图片时将图片设置为ImageView背景的方法
2021-12-14 14:58:38