C# 未处理的异常:无法从关闭的文本阅读器中读取
C# Unhandled Exception: Cannot Read from a Closed Textreader
所以我试图让我的程序从文本文件中读取并在命令提示符中显示该文本文件中的内容,就像这样...
但是然后程序只能读取文件的一行,然后抛出这个错误异常
我已经尝试查看他们抱怨的三个 .cs 文件的行。我尝试调试和编辑我的代码,但我做的越多,情况就越糟,所以我非常迷茫和困惑,老实说,我现在不知道要解决什么问题。
这些是他们抱怨的代码行,下面是实际的代码文件。
FileReader.cs 的第 42 行 --->
return streamReader.ReadLine();
FileReader.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace homework2
{
#region File Reader Class
// ___________________//
// FILE READER //
//____________________//
class FileReader : Reader
{
private StreamReader streamReader;
#region File Reader File Accessing Method
// Accessing the file
public FileReader(string fileName)
{
streamReader = System.IO.File.OpenText(fileName);
if (streamReader == null)
throw new Exception("OpenRead() failed for file " + fileName);
}
#endregion
#region Read Method
// Read Method
public override string Read()
{
// get and return a single line of text
return streamReader.ReadLine();
}
#endregion
#region Close Method
// Close Method
public override void Close()
{
streamReader.Close();
}
#endregion
}
#endregion
}
MorgReader.cs 的第 48 行 --->
while ((Box = Wrapped.Read()) == null)
MorgReader.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using homework2;
namespace homework2
{
#region Morg Reader Class
//___________________//
// MORG READER //
//___________________//
class MorgReader : ReaderDecorator
{
MorgFactory Fact;
public MorgReader(MorgFactory fact, Reader wrapped) : base(wrapped)
{
Fact = fact;
}
public override string Read()
{
return Wrapped.Read();
}
public override void Close()
{
Wrapped.Close();
}
public Morg ReadMorg()
{
#region splitting the text string
// A way to organize the block of text
string Box = " ";
while ((Box = Wrapped.Read()) == null)
return null;
string[] Part = Box.Split(',');
#endregion
#region Displaying each line of text from Morg Reader File to Morg factory
// Translate the info from Morg file to Morg factory to the program
Morg FactMorg = Fact.CreateMorg();
FactMorg.setName(Part[0]);
#region Converting location string to its variable
// A way to convert the string location to the current variable for location
Location FactLoc;
FactLoc = new Location();
FactLoc.X = Convert.ToInt32(Part[1]);
FactLoc.Y = Convert.ToInt32(Part[2]);
#endregion
/* FactMorg.Move(FactLoc.X, FactLoc.Y); */
FactMorg.setLocation(FactLoc);
FactMorg.setMovement(Part[3]);
FactMorg.setFeeding(Part[4]);
#endregion
return FactMorg;
}
}
#endregion
}
Program.cs 的第 33 行 --->
while ((NewMorg = MyReader.ReadMorg()) != null)
Program.cs:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace homework2
{
#region Main Program
class Program
{
static void Main(string[] args)
// ____________________ //
// MAIN PROGRAM //
//______________________//
{
MorgReader MyReader = (new MorgReader(new MyMorgFactory(),new FileReader("morgs.txt")));
Morg NewMorg = new Morg("");
while ((NewMorg = MyReader.ReadMorg()) != null)
{
string NewMorgName = NewMorg.getName();
Location NewMorgXY = NewMorg.getLocation();
string NewMorgMovement = NewMorg.getMovement();
string NewMorgFeeding = NewMorg.getFeeding();
Console.WriteLine(NewMorgName + " " + NewMorgXY.X + " " + NewMorgXY.Y + " " + NewMorgMovement + " " + NewMorgFeeding);
MyReader.Close();
}
//MorgTypeA Morg1 = new MorgTypeA("Axel, the Axe-shaped Morg");
//MorgTypeB Morg2 = new MorgTypeB("Bael, the Dark Morg");
//MorgTypeC Morg3 = new MorgTypeC("Corona, the Light Morg");
//Simulator morgGame = new Simulator(Morg1, Morg2, Morg3);
//morgGame.run();
}
}
#endregion
}
这是我第一次使用 C#,对此我还是很陌生。感谢您的大力帮助。
您将在 while 循环的第一次迭代中关闭 reader。
public static void Main(string[] args)
{
//some stuff
while ((NewMorg = MyReader.ReadMorg()) != null)
{
string NewMorgName = NewMorg.getName();
Location NewMorgXY = NewMorg.getLocation();
string NewMorgMovement = NewMorg.getMovement();
string NewMorgFeeding = NewMorg.getFeeding();
Console.WriteLine(NewMorgName + " " + NewMorgXY.X + " " + NewMorgXY.Y + " " + NewMorgMovement + " " + NewMorgFeeding);
//MyReader.Close(); -> this line put it out from the while loop
}
//put it here
MyReader.Close();
//other stuff
}
所以我试图让我的程序从文本文件中读取并在命令提示符中显示该文本文件中的内容,就像这样...
但是然后程序只能读取文件的一行,然后抛出这个错误异常
我已经尝试查看他们抱怨的三个 .cs 文件的行。我尝试调试和编辑我的代码,但我做的越多,情况就越糟,所以我非常迷茫和困惑,老实说,我现在不知道要解决什么问题。
这些是他们抱怨的代码行,下面是实际的代码文件。
FileReader.cs 的第 42 行 --->
return streamReader.ReadLine();
FileReader.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace homework2
{
#region File Reader Class
// ___________________//
// FILE READER //
//____________________//
class FileReader : Reader
{
private StreamReader streamReader;
#region File Reader File Accessing Method
// Accessing the file
public FileReader(string fileName)
{
streamReader = System.IO.File.OpenText(fileName);
if (streamReader == null)
throw new Exception("OpenRead() failed for file " + fileName);
}
#endregion
#region Read Method
// Read Method
public override string Read()
{
// get and return a single line of text
return streamReader.ReadLine();
}
#endregion
#region Close Method
// Close Method
public override void Close()
{
streamReader.Close();
}
#endregion
}
#endregion
}
MorgReader.cs 的第 48 行 --->
while ((Box = Wrapped.Read()) == null)
MorgReader.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using homework2;
namespace homework2
{
#region Morg Reader Class
//___________________//
// MORG READER //
//___________________//
class MorgReader : ReaderDecorator
{
MorgFactory Fact;
public MorgReader(MorgFactory fact, Reader wrapped) : base(wrapped)
{
Fact = fact;
}
public override string Read()
{
return Wrapped.Read();
}
public override void Close()
{
Wrapped.Close();
}
public Morg ReadMorg()
{
#region splitting the text string
// A way to organize the block of text
string Box = " ";
while ((Box = Wrapped.Read()) == null)
return null;
string[] Part = Box.Split(',');
#endregion
#region Displaying each line of text from Morg Reader File to Morg factory
// Translate the info from Morg file to Morg factory to the program
Morg FactMorg = Fact.CreateMorg();
FactMorg.setName(Part[0]);
#region Converting location string to its variable
// A way to convert the string location to the current variable for location
Location FactLoc;
FactLoc = new Location();
FactLoc.X = Convert.ToInt32(Part[1]);
FactLoc.Y = Convert.ToInt32(Part[2]);
#endregion
/* FactMorg.Move(FactLoc.X, FactLoc.Y); */
FactMorg.setLocation(FactLoc);
FactMorg.setMovement(Part[3]);
FactMorg.setFeeding(Part[4]);
#endregion
return FactMorg;
}
}
#endregion
}
Program.cs 的第 33 行 --->
while ((NewMorg = MyReader.ReadMorg()) != null)
Program.cs:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace homework2
{
#region Main Program
class Program
{
static void Main(string[] args)
// ____________________ //
// MAIN PROGRAM //
//______________________//
{
MorgReader MyReader = (new MorgReader(new MyMorgFactory(),new FileReader("morgs.txt")));
Morg NewMorg = new Morg("");
while ((NewMorg = MyReader.ReadMorg()) != null)
{
string NewMorgName = NewMorg.getName();
Location NewMorgXY = NewMorg.getLocation();
string NewMorgMovement = NewMorg.getMovement();
string NewMorgFeeding = NewMorg.getFeeding();
Console.WriteLine(NewMorgName + " " + NewMorgXY.X + " " + NewMorgXY.Y + " " + NewMorgMovement + " " + NewMorgFeeding);
MyReader.Close();
}
//MorgTypeA Morg1 = new MorgTypeA("Axel, the Axe-shaped Morg");
//MorgTypeB Morg2 = new MorgTypeB("Bael, the Dark Morg");
//MorgTypeC Morg3 = new MorgTypeC("Corona, the Light Morg");
//Simulator morgGame = new Simulator(Morg1, Morg2, Morg3);
//morgGame.run();
}
}
#endregion
}
这是我第一次使用 C#,对此我还是很陌生。感谢您的大力帮助。
您将在 while 循环的第一次迭代中关闭 reader。
public static void Main(string[] args)
{
//some stuff
while ((NewMorg = MyReader.ReadMorg()) != null)
{
string NewMorgName = NewMorg.getName();
Location NewMorgXY = NewMorg.getLocation();
string NewMorgMovement = NewMorg.getMovement();
string NewMorgFeeding = NewMorg.getFeeding();
Console.WriteLine(NewMorgName + " " + NewMorgXY.X + " " + NewMorgXY.Y + " " + NewMorgMovement + " " + NewMorgFeeding);
//MyReader.Close(); -> this line put it out from the while loop
}
//put it here
MyReader.Close();
//other stuff
}