2013-10-08 294 views
1

我正在尝试从使用C#的txt文件读取和写入。目前,我正在编写一个程序,该程序读取列表中的名称,向用户显示它们,请求另一个名称,然后将该名称添加到列表中。阅读很好,但写作存在一些问题。从txt文件读取和写入

代码编译罚款使用CSC,它执行罚款,但之后我键入名称添加并按下回车键,我得到一个窗口弹出称

FileIO.exe遇到问题和需要关闭。

任何想法是什么问题是?

using System; 
using System.IO; 

public class Hello1 
{ 
    public static void Main() 
    { 
     int lines = File.ReadAllLines("Name.txt").Length; 
     string[] stringArray = new string[lines + 1]; 
     StreamReader reader = new StreamReader("Name.txt"); 
     for(int i = 1;i <=lines;i++){ 
      stringArray[i-1] = reader.ReadLine(); 
     } 
     for (int i = 1;i <=stringArray.Length;i++){ 
      Console.WriteLine(stringArray[i-1]); 
     } 
     Console.WriteLine("Please enter a name to add to the list."); 
     stringArray[lines] = Console.ReadLine(); 
     using (System.IO.StreamWriter writer = new System.IO.StreamWriter("Name.txt", true)){ 
      writer.WriteLine(stringArray[lines]); 
     } 
    } 
} 
+1

也使用'using'语句给读者。 '使用(var reader = new StreamReader(“Name.txt”)){...}' –

+0

在你重新打开它之前关闭流。更好地使用“使用”语句 –

+2

为什么你使用'File.ReadAllLines',然后*只是*取长度,然后再读一遍?有很多简单的方法来实现这个... –

回答

-2

使用reader.ReadToEnd功能这样的,不要忘记关闭的读者,当你完成:

StreamReader reader = new StreamReader("Name.txt"); 
string content = reader.ReadToEnd(); 
reader.Close(); 

你会得到异常的原因是因为你不关闭读者阅读后。所以你不能先写入Close();方法。

您还可以使用using语句,而不是关闭它是这样的:

using (StreamReader reader = new StreamReader("Name.txt")){ 
    string content = reader.ReadToEnd(); 
}; 
+0

你应该使用'using'语句 - 我想你已经错过了问题的要点,即逐行读取文件。 –

+0

不关闭阅读器不会导致异常,只是泄漏资源。 – Servy

+0

谢谢乔恩,还补充说! Servy当它仍然开放时试图写信给它。 – Dimo

0

如果你想从文件中读取所有行到一个数组,你可以简单地使用:

string[] lines = File.ReadAllLines("Name.txt"); 

并使用该数组。

2

由于您没有关闭您的reader,您只能在将文件读取到Array后将其放置reader.Close();

更好的是使用using声明,因为StreamReader使用IDisposable接口,这将确保关闭流以及处理它。

string[] stringArray = new string[lines + 1]; 
using (StreamReader reader = new StreamReader("Name.txt")) 
{ 
    for (int i = 1; i <= lines; i++) 
    { 
     stringArray[i - 1] = reader.ReadLine(); 
    } 
} 

只是侧面说明

你使用File.ReadAllLines只是为了获得Length ???,你可以fillup喜欢你的数组:

string[] stringArray = File.ReadAllLines("Name.txt"); 

,而不是通过StreamReader去。

2

怎么样,我们简化了这个有点:

foreach (var line in File.ReadLines("Name.txt")) 
{ 
    Console.WriteLine(line); 
} 
Console.WriteLine("Please enter a name to add to the list."); 
var name = Console.ReadLine(); 
File.AppendLine("Name.txt", name): 

现在你不与IO处理可言,因为你通过利用这些静态方法完全把它留给框架。

+0

为什么要读这个文件开始?你以后忽略字符串数组... –

+0

@Jon,你可以用Console.WriteLine整个文件,也许File.ReadAllText? –

+0

啊,我错过了它是倾倒在控制台 - 对不起。请注意,您的代码目前在最后一次迭代时会失败 - 您需要使用<<而不是<= <。更好,使用'foreach'。更好的是,使用'foreach'与'File.ReadLines' ... –

1

这是很好的,以确保您在一个控制台应用程序的顶层学习任何异常:

public class Hello1 
{ 
    public static void Main() 
    { 
     try 
     { 
      // whatever 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("Exception!); 
      Console.WriteLine(ex.ToString()); 
     } 
     finally 
     { 
      Console.Write("Press ENTER to exit: "); 
      Console.ReadLine(); 
     } 
    } 
} 

这样一来,你就知道为什么应用不得不关闭。

此外,你需要有你的StreamReaderusing块。