File.ReadAllLines(FileName)数组大小限制

编程入门 行业动态 更新时间:2024-10-09 04:23:58
本文介绍了File.ReadAllLines(FileName)数组大小限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果我使用:

字符串 FileName = " MyFile.txt" ; 字符串 [] DataFile = File.ReadAllLines(FileName);

现在说文件有5行... 因此,DataFile数组的最后一个文件行位于4. 如果我想在DataFile [5]处添加一行(它位于数组的底部,并且超过文件位置的末尾),则会得到一个OutOfRangeException,这是可以理解的.做我想做的最好的方法是什么?

解决方案

正如GanesanSenthilvel所说,使用List< string>而是使用Array.Resize()方法. 以下是一些示例:

列表< string> dataFile = 新列表< string>(); dataFile.AddRange(File.ReadAllLines(FileName));

那么您可以用相同的方式添加其他文件行

dataFile.AddRange(File.ReadAllLines(SomeOtherFile));

字符串 [] data = 新 string [] {" 等等"," 等等等等"," 等等等等"}; dataFile.AddRange(data);

或一行

dataFile.Add(" 某些数据行" ); 或

字符串 someData = " 等等等等"; dataFile.Add(someData);

如果您确实想使用Array.Resize()方法,也可以使用

字符串 [] data = 新 字符串 [ 2 ]; data [ 0 ] = " 一些数据" ; data [ 1 ] = " 更多数据" ; Array.Resize( ref data,data.Length + 1 ); data [ 2 ] = " 甚至还有更多数据" ;

您的目标是将行添加到现有文件的末尾 为什么不使用此代码

StreamWriter sw= File.AppendText("c:\\temp.txt"); sw.WriteLine(); sw.WriteLine("hello"); sw.Flush(); sw.Close();

谢谢 --RA

您可以很好地使用

List< string> </ 字符串 >

以避免OutOfRangeException

If I use:

string FileName = "MyFile.txt"; string[] DataFile = File.ReadAllLines(FileName);

Now say the file has 5 lines... Therefore the DataFile array will have its last file line on 4. If I want to add a line at say DataFile[5] (which is at the bottom of the array and past the end of file position), I get an OutOfRangeException which is understandable. What is the best way to do what I want to do? essentially I want the array to be resized.

解决方案

As GanesanSenthilvel said use a List<string> instead or the Array.Resize() method. Here are some examples:

List<string> dataFile = new List<string>(); dataFile.AddRange(File.ReadAllLines(FileName));

then you could add another files lines in the same way

dataFile.AddRange(File.ReadAllLines(SomeOtherFile));

or

string[] data = new string[] { "blah", "blah blah", "blah blah blah" }; dataFile.AddRange(data);

or a single line

dataFile.Add("some line of data");

or

string someData = "blah blah blah blah"; dataFile.Add(someData);

You could if you really wanted to use the Array.Resize() method i.e.

string[] data = new string[2]; data[0] = "some data"; data[1] = "some more data"; Array.Resize(ref data, data.Length + 1); data[2] = "even more data";

Your Target is add line to the existing file at end why don''t you use this code

StreamWriter sw= File.AppendText("c:\\temp.txt"); sw.WriteLine(); sw.WriteLine("hello"); sw.Flush(); sw.Close();

Thanks --RA

Instead of array of String, you can very well use

List<string></string>

to avoid OutOfRangeException

更多推荐

File.ReadAllLines(FileName)数组大小限制

本文发布于:2023-11-03 23:55:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556421.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   大小   File   ReadAllLines   FileName

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!