您的位置:首页 > 编程语言 > C#

C#控制台基础 使用filestream读取txt文本文件并输出到控制台

2016-09-12 21:43 597 查看
       慈心积善融学习,技术愿为有情学。善心速造多好事,前人栽树后乘凉。我今于此写经验,愿见文者得启发。

1、
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication11
{
class Program
{

static void Main(string[] args)
{
//哈,特意找了一本每天都听的佛家故事
string path = @"C:\Users\Administrator\Desktop\金刚经.txt";

byte[] b = new byte[1024*1024]; //byte字节,B,1KB=1024B,1M=1024K

//这个构造函数超级多,        为了防止抛异常,选择如果文件存在就打开,没有就创建     读文件
FileStream fileRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);

//读取来的数据放在字节数组b中,从头开始读,所以为0.读1024*1024个字节
int r = fileRead.Read(b, 0, b.Length);
//我的文件20023字节,这个r也是20023

//字节数组看不懂,所以要进行编码,转化成string
//这样写的话,你会发现出现错误。的确控制台有输出,但是输出后面有许多空格似得
string contents = Encoding.Default.GetString(b);

Console.WriteLine(contents);
Console.ReadKey();
}
}
}

代码效果图,



2、改进。。。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication11
{
class Program
{

static void Main(string[] args)
{
//哈,特意找了一本每天都听的佛家故事
string path = @"C:\Users\Administrator\Desktop\金刚经.txt";

byte[] b = new byte[1024*1024]; //byte字节,B,1KB=1024B,1M=1024K

//这个构造函数超级多,        为了防止抛异常,选择如果文件存在就打开,没有就创建     读文件
FileStream fileRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);

//读取来的数据放在字节数组b中,从头开始读,所以为0.读1024*1024个字节
int r = fileRead.Read(b, 0, b.Length);
//我的文件20023字节,这个r也是20023

//字节数组看不懂,所以要进行编码,转化成string

//字符串转化的语句要注意!
//按照下面这样写的话,会出现乱码。为什么?因为我的文本是从网络上粘贴而来的
string contents = Encoding.Default.GetString(b, 0, r);

Console.WriteLine(contents);
Console.ReadKey();
}
}
}


代码效果图,



3、代码仅需改进

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication11
{
class Program
{

static void Main(string[] args)
{
//哈,特意找了一本每天都听的佛家故事
string path = @"C:\Users\Administrator\Desktop\金刚经.txt";

byte[] b = new byte[1024*1024]; //byte字节,B,1KB=1024B,1M=1024K

//这个构造函数超级多,        为了防止抛异常,选择如果文件存在就打开,没有就创建     读文件
FileStream fileRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);

//读取来的数据放在字节数组b中,从头开始读,所以为0.读1024*1024个字节
int r = fileRead.Read(b, 0, b.Length);
//我的文件20023字节,这个r也是20023

//字节数组看不懂,所以要进行编码,转化成string

//更改编码格式,就好了.
string contents = Encoding.UTF8.GetString(b, 0, r);

Console.WriteLine(contents);
Console.ReadKey();
}
}
}


代码效果图,



在下挺高兴的,钻研佛道儒三家的思想有些日子了,所以特地从网上找来这个佛家小故事。

转回正题,你认为这样就够了吗?还差东西!

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication11
{
class Program
{

static void Main(string[] args)
{
//哈,特意找了一本每天都听的佛家故事
string path = @"C:\Users\Administrator\Desktop\金刚经.txt";

byte[] b = new byte[1024*1024]; //byte字节,B,1KB=1024B,1M=1024K

//这个构造函数超级多,        为了防止抛异常,选择如果文件存在就打开,没有就创建     读文件
FileStream fileRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);

//读取来的数据放在字节数组b中,从头开始读,所以为0.读1024*1024个字节
int r = fileRead.Read(b, 0, b.Length);
//我的文件20023字节,这个r也是20023

//字节数组看不懂,所以要进行编码,转化成string

fileRead.Close();
fileRead.Dispose();

 //更改编码格式,就好了.
string contents = Encoding.UTF8.GetString(b, 0, r);

Console.WriteLine(contents);
Console.ReadKey();
}
}
}


多写,多写,多实践。

感恩曾经帮助过 心少朴 的人。

C#优秀,值得学习。Winform,WPF 都可以关注一下,眼界要开阔。

Visual Studio IDE很好用,推荐!
注:此文是自学笔记所生,质量中等,故要三思而后行。新手到此,不可照搬,应先研究其理象数,待能变通之时,自然跳出深坑。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐