我有关于File.ReadLines()和File.ReadAllLines()的查询.它们之间有什么区别.我有文本文件,其中包含行数据.File.ReadAllLines()返回数组和使用File.ReadLines().ToArray();也将得到相同的结果.那么这些方法有任何性能差异吗?
string[] lines = File.ReadLines("C:\\mytxt.txt").ToArray();
Run Code Online (Sandbox Code Playgroud)
要么
string[] lines = File.ReadAllLines("C:\\mytxt.txt");
Run Code Online (Sandbox Code Playgroud) 新的Visual Studio 2012抱怨我一直使用的常见代码组合.我知道这似乎有点矫枉过正,但我在我的代码中做了以下'只是为了确定'.
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (var sr = new StreamReader(fs))
{
// Code here
}
}
Run Code Online (Sandbox Code Playgroud)
Visual Studio正警告我,我不止一次处理fs.所以我的问题是这样,写这个的正确方法是:
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var sr = new StreamReader(fs);
// do stuff here
}
Run Code Online (Sandbox Code Playgroud)
或者我应该这样做(或者没有提到的其他变体).
var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using (var sr = new StreamReader(fs))
{
// Code here
}
Run Code Online (Sandbox Code Playgroud)
我在StackOverflow中搜索了几个问题,但没有找到直接解决此组合的最佳实践的问题.
谢谢!
更新: PowerShell 5似乎解决了以下错误.该错误仍然存在于3和4中.因此,除非您运行PowerShell 2或5,否则不要使用管道处理任何大型文件.
请考虑以下代码段:
function Get-DummyData() {
for ($i = 0; $i -lt 10000000; $i++) {
"This is freaking huge!! I'm a ninja! More words, yay!"
}
}
Get-DummyData | Out-Null
Run Code Online (Sandbox Code Playgroud)
这将导致PowerShell内存使用量无法控制地增长.执行Get-DummyData | Out-Null几次后,我看到PowerShell内存使用量一直达到4 GB.
根据ANTS Memory Profiler,我们在垃圾收集器的终结队列中有很多东西.当我打电话时[GC]::Collect(),内存从4 GB变为仅70 MB.因此,严格来说,我们没有内存泄漏.
现在,[GC]::Collect()当我完成一个长期的管道操作时,能够打电话对我来说还不够好.我需要在管道操作期间进行垃圾收集.但是,如果我尝试[GC]::Collect()在管道执行时调用...
function Get-DummyData() {
for ($i = 0; $i -lt 10000000; $i++) {
"This is freaking huge!! I'm a ninja! More words, yay!"
if ($i …Run Code Online (Sandbox Code Playgroud) 我搜索过,只找到了控制台的这些信息,但我想知道是否可以将本地机器上的文件中的文本读入代码,格式化并显示在屏幕上?我们有一个带有一些法律术语的文本文件,可以定期更新,而不是让用户筛选代码,我们只想更新文本文件并在线申请更改.
谢谢!
编辑:感谢大家的评论,这是一个有要求的编辑.该程序位于C#ASP.NET网站.我已经阅读了许多关于在控制台中完成此操作的文章,但我不确定如何让它对我有用.再次感谢大家的贡献.
我们可以通过使用StreamReader或使用来读取文件File.ReadAllLines.
例如,我想将每一行加载到每一行List或string[]进行进一步操作.
string[] lines = File.ReadAllLines(@"C:\\file.txt");
foreach(string line in lines)
{
//DoSomething(line);
}
Run Code Online (Sandbox Code Playgroud)
要么
using (StreamReader reader = new StreamReader("file.txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
//DoSomething(line); or //save line into List<string>
}
}
//if list is created loop through list here
Run Code Online (Sandbox Code Playgroud)
应用程序遇到不同大小的文本文件.哪个可以从少数增长KBs到MBs偶尔.
我的问题是,哪一个是首选方式,为什么一个应该优先于其他方式?
我正在做一些关于收益率回归性能的测试,我发现它比正常回报慢.
我测试了值变量(int,double等)和一些引用类型(字符串等)......并且两种情况下的yield return都较慢.为什么要用呢?
看看我的例子:
public class YieldReturnTeste
{
private static IEnumerable<string> YieldReturnTest(int limite)
{
for (int i = 0; i < limite; i++)
{
yield return i.ToString();
}
}
private static IEnumerable<string> NormalReturnTest(int limite)
{
List<string> listaInteiros = new List<string>();
for (int i = 0; i < limite; i++)
{
listaInteiros.Add(i.ToString());
}
return listaInteiros;
}
public static void executaTeste()
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
List<string> minhaListaYield = YieldReturnTest(2000000).ToList();
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, …Run Code Online (Sandbox Code Playgroud) 我想带一个包含很多行的文本文件并将其转换为数组.例如,如果文本文件是:
第1行
第2行
第3行
4号线
它会导致
string[] stringList = { "Line 1", "Line 2", "Line 3", "Line 4" };
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
我试过这个:
string line;
string[] accountList;
using (StreamReader file = new StreamReader(accountFileLocation.Text))
{
while (line = file.ReadLine() != null)
{
stringList += line;
}
}
Run Code Online (Sandbox Code Playgroud)
然而,错误:
无法将类型'bool'隐式转换为'string'
无法将'string'类型转换为'string []'
无法将类型'string'隐式转换为'string []'
众所周知,磁盘 I\O 很昂贵。我通常使用 C# 从 .txt 文件中一次读取一行。我突然想到,如果 C# 为您提供一种一次读取 100 行的方法,它需要更少的磁盘往返次数,从而减少时间。这可能吗?有没有人对如何提高效率有任何想法。我在大约一个小时内浏览了一个 100 万行的文件,一次读取和处理一行。我想看看我是否可以大大减少这个时间。
所以我刚开始接触 C#,几乎没有任何知识,所以这对我来说更适合学习而不是实际使用。因此,我真正想知道的是如何让我的代码按照我的方式工作,即使有更简单/更快/更智能的解决方案。
所以我想做的是创建一个字符串数组,并使用一个循环从文本文件的每一行中读取到数组的相应元素中。这就是我在这里尝试做的事情,我很想听听您对此有什么解决方案。
{
class Program
{
static void Main(string[] args)
{
StreamReader ki = new StreamReader("kiserlet.txt");
string[] a = new string[15];
Console.ReadLine();
int y = 0;
int n = 0;
for (int i = 0; i > 15; i++)
{
a[n] = Convert.ToString(ki.ReadLine());
n++;
}
for (int x = 0;x > 15;x++)
{
Console.WriteLine(a[y]);
y++;
}
Console.ReadLine();
ki.Close();
}
}
}
Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×2
text-files ×2
arrays ×1
asp.net ×1
disk-io ×1
dispose ×1
memory ×1
powershell ×1
readline ×1
stream ×1
using ×1
winforms ×1
yield-return ×1