文件 IO 流读取器出现问题 +流写入器 C#

发布于 2024-12-06 02:00:23 字数 5055 浏览 0 评论 0原文

大家好,我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗失的美好 2024-12-13 02:00:23

当我读写时,它只在一行上进行。有办法吗
推进流读取器/流写入器?

是的 - 使用 streamWriter.WriteLine() 而不是 streamWriter.Write();

您正在编写的内容看起来像用“~”分隔的数据 - 您可能想考虑编写out CSV = 逗号分隔值,这是相似的并且几乎是标准的。

另外,您应该使用强类型集合,而不是 ArrayList - 在您的情况下,根据您的类结构,您可以使用 List (如果 AutomobileMotorcycle 都继承自 Vehicle)。

When I read and write it is only doing it on one line. Is there a way
to advance the streamreader/streamwriter?

Yes - use streamWriter.WriteLine() instead of streamWriter.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 use List<Vehicle> (if Automobile and Motorcycle both inherit from Vehicle).

暖风昔人 2024-12-13 02:00:23

问题在于这一行,

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

当遇到空行时,循环将退出,因为 IsNullOrEmpty 方法返回 true。尝试下面代替..

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

The issue is with this line

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

The loop exits when it meets a blank line because IsNullOrEmpty method returns true. Try below instead..

while ((tempString = streamReader.ReadLine()) != null)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文