C# 从磁盘中读取文件
读取txt文件
------读取的数据比较小的时候:
如果你要读取的文件内容不是很多,可以使用 File.ReadAllText(filePath) 或指定编码方式 File.ReadAllText(FilePath, Encoding)的方法。它们都一次性将文本内容全部读完,并返回一个包含全部文本内容的字符串
用string接收
string str1 = File.ReadAllText(@"c:\temp\a.txt"); //也可以指定编码方式
string str2 = File.ReadAllText(@"c:\temp\a.txt", Encoding.ASCII);
也可以使用方法File.ReadAllLines,该方法一次性读取文本内容的所有行,返回一个字符串数组,数组元素是每一行的内容
string[] strs1 = File.ReadAllLines(@"c:\temp\a.txt");
// 也可以指定编码方式
string[] strs2 = File.ReadAllLines(@"c:\temp\a.txt", Encoding.ASCII);
-----读取数据比较大的时候,采用流的方式:
当文本的内容比较大时,我们就不要将文本内容一次性读完,而应该采用流(Stream)的方式来读取内容。
.Net为我们封装了StreamReader类,它旨在以一种特定的编码从字节流中读取字符。StreamReader类的方法不是静态方法,所以要使用该类读取文件首先要实例化该类,在实例化时,要提供读取文件的路径。
StreamReader sR1 = new StreamReader(@"c:\temp\a.txt");
// 读一行
string nextLine = sR.ReadLine();
// 同样也可以指定编码方式
StreamReader sR2 = new StreamReader(@"c:\temp\a.txt", Encoding.UTF8);
FileStream fS = new FileStream(@"C:\temp\a.txt", FileMode.Open, FileAccess.Read, FileShare.None);
StreamReader sR3 = new StreamReader(fS);
StreamReader sR4 = new StreamReader(fS, Encoding.UTF8);
FileInfo myFile = new FileInfo(@"C:\temp\a.txt");
// OpenText 创建一个UTF-8 编码的StreamReader对象
StreamReader sR5 = myFile.OpenText();
// OpenText 创建一个UTF-8 编码的StreamReader对象
StreamReader sR6 = File.OpenText(@"C:\temp\a.txt");
获取到大的文件后,都是流的返回形式
可以用流的读取方法读出数据,返回类型是String类型
// 读一行
string nextLine = sR.ReadLine();
// 读一个字符
int nextChar = sR.Read();
// 读100个字符
int n = 100;
char[] charArray = new char[n];
int nCharsRead = sR.Read(charArray, 0, n);
// 全部读完
string restOfStream = sR.ReadToEnd();
使用完StreamReader之后,不要忘记关闭它: sR.Close();
假如我们需要一行一行的读,将整个文本文件读完,下面看一个完整的例子:

StreamReader sR = File.OpenText(@"C:\temp\a.txt");
string nextLine;
while ((nextLine = sR.ReadLine()) != null)
{
Console.WriteLine(nextLine);
}
sR.Close();

C# 从磁盘中读取文件的更多相关文章
- 文件_ _android从资源文件中读取文件流并显示的方法
======== 1 android从资源文件中读取文件流并显示的方法. 在android中,假如有的文本文件,比如TXT放在raw下,要直接读取出来,放到屏幕中显示,可以这样: private ...
- 用adb pull命令从android系统中读取文件失败的原因及解决办法
问题:使用adb pull命令从android系统中读取文件失败.显示:Permission denied 原因:是由于文件权限原因引起. 使用ls -l命令查看android系统中的 ...
- Java中读取文件
Java中读取文件,去除一些分隔符,保存在多维数组里面 public void readFile(String filePath) { File file=new File(filePath); Ar ...
- 如何配置一个路径,能够既适合Linux平台,又适合Windows平台,可以从这个路径中读取文件
如何配置一个路径,能够既适合Linux平台,又适合Windows平台,可以从这个路径中读取文件? 目的:就是希望在项目的配置文件中配上一样的路径,不管协作者使用的是什么平台,都能够读到文件. 比如:L ...
- PHP中读取文件的几个方法
整理了一下PHP中读取文件的几个方法,方便以后查阅. 1.fread string fread ( int $handle , int $length ) fread() 从 handle 指向的文件 ...
- R中读取文件,找不到路径问题 No such file or directory
R中读取文件,找不到路径问题 No such file or directory 近日,读取文件时.出现例如以下问题 > passenger = read.csv('internationa ...
- php中读取文件内容的几种方法。(file_get_contents:将文件内容读入一个字符串)
php中读取文件内容的几种方法.(file_get_contents:将文件内容读入一个字符串) 一.总结 php中读取文件内容的几种方法(file_get_contents:将文件内容读入一个字符串 ...
- Spring 中读取文件-ResourceLoaderAware
Spring 中读取文件-ResourceLoaderAware 概述 Spring ResourceLoader为我们提供了一个统一的getResource()方法来通过资源路径检索外部资源.从而将 ...
- python中读取文件的read、readline、readlines方法区别
#读取文件所有内容,返回字符串对象,python默认以文本方式读取文件,遇到结束符读取结束. fr = open('lenses.txt')read = fr.read()print(type(rea ...
随机推荐
- 【转载】Failed to load class "org.slf4j.impl.StaticLoggerBinder".问题解决
在进行hibernate配置好后运行测试类的时候出现: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" ...
- @SpringBootApplication cannot be resolved to a type In STS
@SpringBootApplication cannot be resolved to a type In STS 学习了:https://stackoverflow.com/questions/4 ...
- POJ 3207 Ikki's Story IV - Panda's Trick(2-sat)
POJ 3207 Ikki's Story IV - Panda's Trick id=3207" target="_blank" style=""& ...
- [LeetCode]Median of Two Sorted Arrays 二分查找两个有序数组的第k数(中位数)
二分.情况讨论 因为数组有序,所以能够考虑用二分.通过二分剔除掉肯定不是第k位数的区间.如果数组A和B当前处理的下标各自是mid1和mid2.则 1.假设A[mid1]<B[mid2], ①.若 ...
- JavaScript编程随笔
尽管说用JS非常多年了,可是却一直停留在肤浅的阶段,对JS的机制原理依旧是一知半解,比如:闭包.尽管能说出一二.却不能说出三四,确实羞愧.近期恶补一番.并将比較与大家分享,希望对大家有些帮助. 闭包 ...
- zoj_3735,dp,长沙站j题
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...
- Objects are mutable
We can change the state of an object by making an assignment to one of its attributes. For example, ...
- Car Talk1
This question is based on a Puzzler that was broadcast on the radioprogram Car Talk1: “I was driving ...
- BZOJ 3503 高斯消元
思路: 高斯消元就好啦 注意每个格子最多只能和4个相邻 所以是 n*m*n*m*5 的 并不会TLE //By SiriusRen #include <cstdio> #include & ...
- POJ 3669 简单BFS
标号 搜 完了-- //By SiriusRen #include <queue> #include <cstdio> #include <cstring> #in ...