文件 IO 流读取器出现问题 +流写入器 C#
大家好,我对 C# 有点陌生。我正在尝试做一些文件 io 的事情。当我读写时,它只在一行上进行。有没有办法提升流读取器/流写入器?这是学校作业。这是我到目前为止所拥有的。非常感谢。
enter code her class FileIO
{
public static void Save(ArrayList vehicleList)
{
StreamWriter streamWriter;
FileStream file = new FileStream(Constants.fileName, FileMode.OpenOrCreate);
streamWriter = new StreamWriter(file);
String typeOfVehicle = " ";
String model = " ";
String manufactuer = " ";
Int32 year = 0;
Int32 vin = 0;
Double price = 0;
String purchaseDate = " ";
Int32 currentOdometerReading = 0;
Double sizeOfEngine = 0;
String typeOfMotorCycle = " ";
Int32 numOfDoors = 0;
String typeOfFuel = " ";
Double cargoCapacity = 0;
Double towingCapacity = 0;
foreach (CommonItem vehicle in vehicleList)
{
typeOfVehicle = vehicle.TypeOfVehicle;
model = vehicle.Model;
manufactuer = vehicle.Manufactuer;
year = vehicle.Year;
vin = vehicle.Vin;
price = vehicle.InitialPrice;
purchaseDate = vehicle.PurchaseDate;
currentOdometerReading = vehicle.CurrentOdometerReading;
sizeOfEngine = vehicle.EngineSize;
streamWriter.Write(typeOfVehicle + "~" + manufactuer + "~" + model + "~" + year.ToString() + "~" + vin.ToString() + "~" + price.ToString() + "~" + purchaseDate + "~" + currentOdometerReading.ToString() + "~" + sizeOfEngine.ToString() + "~");
switch (typeOfVehicle)
{
case "Automobile":
numOfDoors = ((Automobile)vehicle).NumberOfDoors;
typeOfFuel = ((Automobile)vehicle).TypeOfFuel;
streamWriter.Write(numOfDoors + "~" + typeOfFuel + "~");
break;
case "Motorcycle":
typeOfMotorCycle = ((Motorcycle)vehicle).Type;
streamWriter.Write(typeOfMotorCycle + "\n");
break;
case "Truck":
cargoCapacity = ((Truck)vehicle).CargoCapacity;
towingCapacity = ((Truck)vehicle).TowingCapacity;
streamWriter.Write(cargoCapacity + "~" + towingCapacity);
break;
}
streamWriter.Write("\n");
}
streamWriter.Close();
}
public static ArrayList Load()
{
ArrayList vehicles = new ArrayList();
FileStream file = new FileStream(Constants.fileName, FileMode.OpenOrCreate);
StreamReader streamReader = new StreamReader(file);
String typeOfVehicle = " ";
String model = " ";
String manufactuer = " ";
Int32 year = 0;
Int32 vin = 0;
Double price = 0;
String purchaseDate = " ";
Int32 currentOdometerReading = 0;
Double sizeOfEngine = 0;
String typeOfMotorCycle = " ";
Int32 numOfDoors = 0;
String typeOfFuel = " ";
Double cargoCapacity = 0;
Double towingCapacity = 0;
String tempString = " ";
while (!String.IsNullOrEmpty(tempString = streamReader.ReadLine()))
{
Int32 temp;
String [] split = tempString.Split('~');
temp = split.Length;
typeOfVehicle = split[0];
manufactuer = split[1];
model = split[2];
year = Convert.ToInt32(split[3]);
vin = Convert.ToInt32(split[4]);
price = Convert.ToDouble(split[5]);
purchaseDate = split[6];
currentOdometerReading = Convert.ToInt32(split[7]);
sizeOfEngine = Convert.ToDouble(split[8]);
if (typeOfVehicle == "Automobile")
{
numOfDoors = Convert.ToInt32(split[9]);
typeOfFuel = split[10];
Automobile car = new Automobile(manufactuer, model, year, vin, price, purchaseDate, currentOdometerReading, sizeOfEngine, numOfDoors, typeOfFuel);
VehicleCount.IncreaseCarCount();
vehicles.Add(car);
}
else if (typeOfVehicle == "Motorcycle")
{
typeOfMotorCycle = split[9];
Motorcycle bike = new Motorcycle(manufactuer, model, year, vin, price, purchaseDate, currentOdometerReading, sizeOfEngine, typeOfMotorCycle);
VehicleCount.IncreaseBikeCount();
vehicles.Add(bike);
}
else
{
cargoCapacity = Convert.ToDouble(split[9]);
towingCapacity = Convert.ToDouble(split[10]);
Truck truck = new Truck(manufactuer, model, year, vin, price, purchaseDate, currentOdometerReading, sizeOfEngine, cargoCapacity, towingCapacity);
VehicleCount.IncreaseTruckCount();
vehicles.Add(truck);
}
}
streamReader.Close();
return vehicles;
}
}
}
Hi guys I am kinda new to C#. I am trying to do some file io stuff. When I read and write it is only doing it on one line. Is there a way to advance the streamreader/streamwriter? It is for a school assignment. Here is what I have so far. Thanks very much.
enter code her class FileIO
{
public static void Save(ArrayList vehicleList)
{
StreamWriter streamWriter;
FileStream file = new FileStream(Constants.fileName, FileMode.OpenOrCreate);
streamWriter = new StreamWriter(file);
String typeOfVehicle = " ";
String model = " ";
String manufactuer = " ";
Int32 year = 0;
Int32 vin = 0;
Double price = 0;
String purchaseDate = " ";
Int32 currentOdometerReading = 0;
Double sizeOfEngine = 0;
String typeOfMotorCycle = " ";
Int32 numOfDoors = 0;
String typeOfFuel = " ";
Double cargoCapacity = 0;
Double towingCapacity = 0;
foreach (CommonItem vehicle in vehicleList)
{
typeOfVehicle = vehicle.TypeOfVehicle;
model = vehicle.Model;
manufactuer = vehicle.Manufactuer;
year = vehicle.Year;
vin = vehicle.Vin;
price = vehicle.InitialPrice;
purchaseDate = vehicle.PurchaseDate;
currentOdometerReading = vehicle.CurrentOdometerReading;
sizeOfEngine = vehicle.EngineSize;
streamWriter.Write(typeOfVehicle + "~" + manufactuer + "~" + model + "~" + year.ToString() + "~" + vin.ToString() + "~" + price.ToString() + "~" + purchaseDate + "~" + currentOdometerReading.ToString() + "~" + sizeOfEngine.ToString() + "~");
switch (typeOfVehicle)
{
case "Automobile":
numOfDoors = ((Automobile)vehicle).NumberOfDoors;
typeOfFuel = ((Automobile)vehicle).TypeOfFuel;
streamWriter.Write(numOfDoors + "~" + typeOfFuel + "~");
break;
case "Motorcycle":
typeOfMotorCycle = ((Motorcycle)vehicle).Type;
streamWriter.Write(typeOfMotorCycle + "\n");
break;
case "Truck":
cargoCapacity = ((Truck)vehicle).CargoCapacity;
towingCapacity = ((Truck)vehicle).TowingCapacity;
streamWriter.Write(cargoCapacity + "~" + towingCapacity);
break;
}
streamWriter.Write("\n");
}
streamWriter.Close();
}
public static ArrayList Load()
{
ArrayList vehicles = new ArrayList();
FileStream file = new FileStream(Constants.fileName, FileMode.OpenOrCreate);
StreamReader streamReader = new StreamReader(file);
String typeOfVehicle = " ";
String model = " ";
String manufactuer = " ";
Int32 year = 0;
Int32 vin = 0;
Double price = 0;
String purchaseDate = " ";
Int32 currentOdometerReading = 0;
Double sizeOfEngine = 0;
String typeOfMotorCycle = " ";
Int32 numOfDoors = 0;
String typeOfFuel = " ";
Double cargoCapacity = 0;
Double towingCapacity = 0;
String tempString = " ";
while (!String.IsNullOrEmpty(tempString = streamReader.ReadLine()))
{
Int32 temp;
String [] split = tempString.Split('~');
temp = split.Length;
typeOfVehicle = split[0];
manufactuer = split[1];
model = split[2];
year = Convert.ToInt32(split[3]);
vin = Convert.ToInt32(split[4]);
price = Convert.ToDouble(split[5]);
purchaseDate = split[6];
currentOdometerReading = Convert.ToInt32(split[7]);
sizeOfEngine = Convert.ToDouble(split[8]);
if (typeOfVehicle == "Automobile")
{
numOfDoors = Convert.ToInt32(split[9]);
typeOfFuel = split[10];
Automobile car = new Automobile(manufactuer, model, year, vin, price, purchaseDate, currentOdometerReading, sizeOfEngine, numOfDoors, typeOfFuel);
VehicleCount.IncreaseCarCount();
vehicles.Add(car);
}
else if (typeOfVehicle == "Motorcycle")
{
typeOfMotorCycle = split[9];
Motorcycle bike = new Motorcycle(manufactuer, model, year, vin, price, purchaseDate, currentOdometerReading, sizeOfEngine, typeOfMotorCycle);
VehicleCount.IncreaseBikeCount();
vehicles.Add(bike);
}
else
{
cargoCapacity = Convert.ToDouble(split[9]);
towingCapacity = Convert.ToDouble(split[10]);
Truck truck = new Truck(manufactuer, model, year, vin, price, purchaseDate, currentOdometerReading, sizeOfEngine, cargoCapacity, towingCapacity);
VehicleCount.IncreaseTruckCount();
vehicles.Add(truck);
}
}
streamReader.Close();
return vehicles;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的 - 使用
streamWriter.WriteLine()
而不是streamWriter.Write();
您正在编写的内容看起来像用“~”分隔的数据 - 您可能想考虑编写out CSV = 逗号分隔值,这是相似的并且几乎是标准的。
另外,您应该使用强类型集合,而不是
ArrayList
- 在您的情况下,根据您的类结构,您可以使用List
(如果Automobile
和Motorcycle
都继承自Vehicle
)。Yes - use
streamWriter.WriteLine()
instead ofstreamWriter.Write();
What you are writing looks like data separated by "~" - you might want to look into writing out CSV = comma-separated values, which is similar and pretty much standard.
Also instead of
ArrayList
you should use a strongly typed collection - in your case depending on your class structure you can useList<Vehicle>
(ifAutomobile
andMotorcycle
both inherit fromVehicle
).问题在于这一行,
当遇到空行时,循环将退出,因为 IsNullOrEmpty 方法返回 true。尝试下面代替..
The issue is with this line
The loop exits when it meets a blank line because IsNullOrEmpty method returns true. Try below instead..