当前位置: 首页 > 面试题库 >

BufferedReader跳过每一行

嵇星海
2023-03-14
问题内容

从CSV读取数据时,它会跳过第二行。我有两个CSV文件,一个用于用户,一个用于属性-密钥ID是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){}}
            }


}

我与以下用户一起运行

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

每个奇数编号都在工作,并且检测到用户已登录,而第二个奇数编号正在跳过。我阅读了一下,但我不认为我有.nextLine()正在像此链接中那样跳过,但是我只在星期五开始使用CSV和BufferedReader!


问题答案:

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

我建议改为使用

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");


 类似资料:
  • 问题内容: 我正在使用以下内容读取文件的各行, 现在,我想跳过读取文件的第一行,并且不想使用计数器行来保持行数。 这该怎么做? 问题答案: 你可以试试这个

  • 我使用下面的读取文件的行, 现在,我想跳过读取文件的第一行,我不想使用计数器行来记录行数。 这要怎么做?

  • 当使用BufferedReader读取文件时,我希望它跳过空行和以“#”开头的行。最终,每个单独的字符都会被添加到数组列表中 除非我弄错了,BufferedReader会自动跳过空行。除此之外,我该怎么做呢? 我能跳过()吗?行的长度可能会有所不同,所以我认为这行不通。

  • 因此,我正在阅读一个文件,其中包含我在前面代码中写到的约会。我想在文本文件中搜索某个日期的约会并将其添加到ArrayList中,但当BufferedReader遍历它时,它会跳过其他行...这是我的代码

  • 对于我正在开发的应用程序,我有一个文本文件,我想使用BufferedReader读取它。文本文件中的前四行可能不相关,所以我不想读这些。我看了BufferedReader上的留档,我看到我可以使用BufferedReader.skip(字节),在那里我输入要跳过的字节数。然而,文本文件中的前四行并不总是包含相同数量的信息,所以我认为这并不真正适合我的目的。你们知道如何以更实际的方式解决这个问题吗?

  • 我正在尝试将XBee输出的段分组到一个变量中,该变量将它们连接起来。我正在使用Processing来编码和编译。我遇到的问题是输出(println)每隔一个字节就跳过一次(也许这是错误的术语)。因此i=4到11的XBee输出应该如下所示: 0,19,162,0,64,121,230,206(这是由十六进制转换的XBee地址)。