c#中文件IO流阅读器+流写入器的问题

本文关键字:问题 中文 文件 IO | 更新日期: 2023-09-27 18:09:05

大家好,我是c#的新手。我正在处理一些文件。当我读和写的时候,它只在一行上做。是否有一种方法来推进流阅读器/流写入器?这是学校的作业。这是我目前所知道的。非常感谢。

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;
    }
}

}

c#中文件IO流阅读器+流写入器的问题

当我读和写它只做一行。有办法吗?推进流阅读器/流写入器?

是-使用streamWriter.WriteLine()代替streamWriter.Write();

你写的东西看起来像用"~"分隔的数据-你可能想看看写CSV =逗号分隔的值,这是类似的,非常标准。

也不是ArrayList,你应该使用强类型集合-在你的情况下,取决于你的类结构,你可以使用List<Vehicle>(如果AutomobileMotorcycle都继承自Vehicle)。

问题在于这一行

while (!String.IsNullOrEmpty(tempString = streamReader.ReadLine()))

当遇到空行时循环退出,因为IsNullOrEmpty方法返回true。

while ((tempString = streamReader.ReadLine()) != null)