且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

BufferedReader跳过每一行

更新时间:2023-02-05 09:28:55

它跳过第二行,因为您每次迭代都读取两行,并且仅使用奇数行。



I建议改用

  while((thisLine = reader.readLine())!= null){//每个循环只能读取一次。 
字符串propertyDetails [] = thisLine.split(,);
if(propertyDetails [0] .equals(userName)){
System.out.print( \nUser: + propertyDetails [0] + \nAddress: + propertyDetails [1] + \nEst。值: + propertyDetails [2]
+ \nLocation猫: + propertyDetails [3] + \n私人住宅: + propertyDetails [4] + \n已付税款: + propertyDetails [5] + \nTax Due: + propertyDetails [6] + breakLine);

}
}
//不在循环之外。
System.out.print( \n没有找到该用户的其他属性\n);


Reading from a CSV and it skips every second line. I have two CSV files, one for users, one for properties - the key ID is user.

    String userName;
    static String breakLine = "\n--------------------\n";
        /**
        * Method to create a new user in a CSV File
        * @param sFileName
        * @param user
        */
   static void writeToCsvFile(String sFileName, User user)
   {
    try
    {
        FileWriter writer = new FileWriter(sFileName, true);


        writer.append(user.displayUserName()); // get username from userinput
        writer.append(","); // tabs to next record

        writer.append(user.getPassword()); //gets password from userinput
        writer.append(","); //tabs to next record
        writer.append("\n");

        writer.flush();
        writer.close();
        System.out.print("\nUser Successfully Created\n");
    }
    catch(IOException e)
    {
         e.printStackTrace();
    } 

   }





   /**
    * Class to read User information from specified CSV file
    * @param sFileName
    * @param user
    * @throws FileNotFoundException
    */
   static boolean readFromCsvFile(String sFileName, User user) throws FileNotFoundException
   {
       String thisLine;
       BufferedReader reader = new BufferedReader(new FileReader(sFileName));


        try
        {

        //  thisLine = reader.readLine();
          //System.out.print(thisLine);


           while((thisLine = reader.readLine()) != null)
                {
                    String userDetails[] = thisLine.split(",");

                    if ((user.displayUserName().equals(userDetails[0])))
                    {
                        System.out.print("\nUser <-" + user.displayUserName() + " -> exists! Logging In\n\n");
                        return true;
                    }

                    else if ((thisLine = reader.readLine()) == null)
                    {
                        System.out.print("\nNo User Details Matching Those Entered Exist. Please Register or Recheck Details.");
                        return false;
                    }
               }

        }
        catch(IOException e)
        {
            System.out.print("\nUser does not exist\n"); 
            e.printStackTrace();
            return false;
        } 


        finally{
            try
            {   reader.close();
            }catch (IOException e){}}
        return false;
            }


   static void writeToPropertyFile(String sFileName, Property property)
   {

    try
    {
        FileWriter writer = new FileWriter(sFileName, true);



        writer.append(property.getUser()); // get username from userinput
        writer.append(","); // tabs to next record

        writer.append(property.getAddress()); //gets address from userinput
        writer.append(","); //tabs to next record

        writer.append(property.getValue()); //gets property value from userinput
        writer.append(","); //tabs to next record

        writer.append(property.getLocation()); //gets property type from userinput
        writer.append(","); //tabs to next record

        writer.append(property.getResidenceStatus()); //gets residence status from userinput
        writer.append(","); //tabs to next record

        writer.append(property.getPaymentStatus()); //gets payment status from userinput
        writer.append(","); //tabs to next record

        writer.append(property.totalTax(property.privateResidence));
        writer.append(",");

        writer.append("\n");

        writer.flush();
        writer.close();
        System.out.print("\nProperty Successfully Saved to " + property.getUser() + "\n");
    }
    catch(IOException e)
    {
         e.printStackTrace();
    } 

   }

/**
 * Method to read and print all properties belonging to a user
 * @param sFileName
 * @param property
 * @param userName
 * @throws FileNotFoundException
 */
static void readFromPropertyFile(String sFileName, String userName) throws FileNotFoundException
{

       String thisLine;
       BufferedReader reader = new BufferedReader(new FileReader(sFileName));


        try
        {
            //thisLine = reader.readLine(); //skips first line in CSV


           while((thisLine = reader.readLine()) != null)
                {

                    String propertyDetails[] = thisLine.split(",");



                    if (propertyDetails[0].equals(userName))
                    {
                    System.out.print("\nUser: " + propertyDetails[0] + "\nAddress: " + propertyDetails[1] + "\nEst. Value: " + propertyDetails[2]
                            + "\nLocation Cat: " + propertyDetails[3] + "\nPrivate Residence: " + propertyDetails[4] + "\nTax Paid: " + propertyDetails[5] +  "\nTax Due: " + propertyDetails[6] + breakLine);

                    }

                    else if ((thisLine = reader.readLine()) == null)
                        System.out.print("\nNo Further Properties Found For This User\n");


               }

        }
        catch(IOException e)
        {
            System.out.print("\nProperties do not exist\n"); 
            e.printStackTrace();
        } 


        finally{
            try
            {   reader.close();
            }catch (IOException e){}}
            }


}

I was running it with the following users

Chris,password
Michelle,password
Zach,password
etc (on seperate lines, seperated by appending \n at the end of the input stage)

Every odd numbered one was working and detecting that the user was logged in, each second one is skipping. I read up and I don't think I have a .nextLine() running skipping as in this link but I only starting using CSVs and BufferedReader on Friday!

BufferedReader readLine skipping every second line

Its skipping every second line because you are reading two lines in every iteration and only using the odd ones.

I suggest instead using

while ((thisLine = reader.readLine()) != null) { // only read once per loop.
    String propertyDetails[] = thisLine.split(",");
    if (propertyDetails[0].equals(userName)) {
        System.out.print("\nUser: " + propertyDetails[0] + "\nAddress: " + propertyDetails[1] + "\nEst. Value: " + propertyDetails[2]
                + "\nLocation Cat: " + propertyDetails[3] + "\nPrivate Residence: " + propertyDetails[4] + "\nTax Paid: " + propertyDetails[5] + "\nTax Due: " + propertyDetails[6] + breakLine);

    }
}
// out side the loop.
System.out.print("\nNo Further Properties Found For This User\n");