开发者

When using stream reader my method is only reading in one line in the file

while (!String.IsNullOrEmpty(tempString = streamReader.ReadLine())) This loop is only iterating once. Any help is appreciated. Full method:

  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;
   开发者_Python百科     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()))
        {
            String[] split = tempString.Split('~');
            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;
    }


Try below.

while ((tempString = streamReader.ReadLine()) != null)
{
    // ...
}

The issue with your current code is the loop exits once it meets a blank line

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


Try this...

using (StreamReader streamReader = new StreamReader(file))
{
    string tempString ;
    while ((tempString = streamReader .ReadLine()) != null)
    {

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜