BufferedReader跳过每一行 [英] BufferedReader skipping each second line

查看:107
本文介绍了BufferedReader跳过每一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从CSV读取数据会跳过第二行。我有两个CSV文件,一个用于用户,一个用于属性-密钥ID是user。

 字符串userName; 
static String breakLine = \n -------------------- \n;
/ **
*在CSV文件中创建新用户的方法
* @param sFileName
* @param用户
* /
静态无效writeToCsvFile(String sFileName,User user)
{
try
{
FileWriter writer = new FileWriter(sFileName,true);


writer.append(user.displayUserName()); //从userinput
writer.append(,);中获取用户名//跳至下一条记录

writer.append(user.getPassword()); //从用户输入
writer.append(,);获取密码//跳转到下一条记录
writer.append( \n);

writer.flush();
writer.close();
System.out.print( \nUser成功创建\n);
}
catch(IOException e)
{
e.printStackTrace();
}

}





/ **
*阅读类用户指定CSV文件中的信息
* @param sFileName
* @param用户
* @throws FileNotFoundException
* /
静态布尔型readFromCsvFile(String sFileName,User user)throws FileNotFoundException
{
字符串thisLine;
BufferedReader reader = new BufferedReader(new FileReader(sFileName));


试试
{

// 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()+->存在!正在登录\n\n);
返回true;
}

else if((thisline = reader.readLine())== null)
{
System.out.print( \n无用户详细信息与输入的内容匹配。请注册或重新检查详细信息。);
返回false;
}
}

}
catch(IOException e)
{
System.out.print( \nUser不存在\n);
e.printStackTrace();
返回false;
}


最后{
试试
{reader.close();
} catch(IOException e){}}
返回false;
}


静态无效writeToPropertyFile(String sFileName,Property property)
{

try
{
FileWriter writer =新的FileWriter(sFileName,true);



writer.append(property.getUser()); //从userinput
writer.append(,);中获取用户名//跳转到下一条记录

writer.append(property.getAddress()); //从用户输入
writer.append(,);中获取地址//跳转到下一条记录

writer.append(property.getValue()); //从userinput
writer.append(,);获取属性值//跳转到下一条记录

writer.append(property.getLocation()); //从userinput
writer.append(,);获取属性类型//跳转到下一条记录

writer.append(property.getResidenceStatus()); //从用户输入
writer.append(,);获得居住状态//跳转到下一条记录

writer.append(property.getPaymentStatus()); //从用户输入
writer.append(,);获取付款状态//跳转到下一条记录

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

writer.append( \n);

writer.flush();
writer.close();
System.out.print( \nProperty已成功保存到 + property.getUser()+ \n);
}
catch(IOException e)
{
e.printStackTrace();
}

}

/ **
*读取和打印属于用户的所有属性的方法
* @param sFileName
* @param属性
* @param userName
* @throws FileNotFoundException
* /
静态void readFromPropertyFile(String sFileName,String userName)抛出FileNotFoundException
{

字符串thisLine;
BufferedReader reader = new BufferedReader(new FileReader(sFileName));


试试
{
// thisLine = reader.readLine(); //跳过CSV中的第一行


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 Cat: + propertyDetails [3] + \ \nPrivate Residence: + propertyDetails [4] + \nTax Paid: + propertyDetails [5] + \nTax Due: + propertyDetails [6] + breakLine);

}

否则,如果((thisLine = reader.readLine())== null)
System.out.print( Non未找到其他属性对于此用户\n);


}

}
catch(IOException e)
{
System.out.print( \nProperties不存在\n);
e.printStackTrace();
}


最后{
试试
{reader.close();
}捕获(IOException e){}}
}


}

我正在与以下用户一起运行

  Chris,密码
Michelle ,密码
Zach,密码
等(在单独的行上,在输入阶段末尾附加\n来分隔)

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

href = https://stackoverflow.com/questions/7200462/bufferedreader-readline-skipping-every-second-line> BufferedReader readLine跳过第二行

解决方案

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



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

这篇关于BufferedReader跳过每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆