大家好我是C#的新手。我正在尝试做一些文件io的东西。当我读写时,它只在一行上完成。有没有办法推进streamreader / streamwriter?这是一个学校作业。这是我到目前为止所拥有的。非常感谢。
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;
}
}
}
答案 0 :(得分:1)
当我读写时,它只在一行上完成。有办法吗? 推进streamreader / streamwriter?
是 - 使用streamWriter.WriteLine()
代替streamWriter.Write();
您所写的内容看起来像是以“〜”分隔的数据 - 您可能需要考虑写出CSV = comma-separated values,这是相似且非常标准的。
而不是ArrayList
您应该使用强类型集合 - 在您的情况下,根据您的类结构,您可以使用List<Vehicle>
(如果Automobile
和Motorcycle
都继承来自Vehicle
)。
答案 1 :(得分:0)
问题在于这一行
while (!String.IsNullOrEmpty(tempString = streamReader.ReadLine()))
循环在遇到空行时退出,因为IsNullOrEmpty方法返回true。请尝试下面..
while ((tempString = streamReader.ReadLine()) != null)