I need to display a certain row from a file

与世无争的帅哥 提交于 2020-07-03 11:56:07

问题


i need to get text from selected line from Txt file and print it on JLabel object I am learning Java and I do not have anyone who can help me with this code, thank everyone who will help

if you need more code from my project, I can send it

public int check(String filename, String str)
    {
        int row=0;
        try{
        FileReader fr=new FileReader(filename);
        BufferedReader br=new BufferedReader(fr);
        while(br.ready())
        {
            if(br.readLine().equals(str))
            {
               br.close();
               return row;

            }
            row=row+1;
        }
        br.close();
        return -1;
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(this, "Error");
        }
        return -1;
    } 

------------------------------------------------------------------------------------------------------

    String str=jComboBoxAdminPUsers.getSelectedItem().toString();
        System.out.println(str);
   int a=check("Pass.txt", str);
   int b=check("Email.txt", str);
   try{
   FileReader fr=new FileReader("Pass.txt");
   BufferedReader br=new BufferedReader(fr);
   String pass="";
   for (int i=0;i<=a;i++)
         pass=br.readLine();
   jLabelPassword.setText(pass);
      br.close();

   FileReader fr1=new FileReader("Email.txt");
   BufferedReader br1=new BufferedReader(fr1);
   String email="";
   for (int i=0;i<=b;i++)
         email=br1.readLine();
   jLabelEmail.setText(email);
      br1.close();
   }
   catch(Exception e)
   {

   }

来源:https://stackoverflow.com/questions/62357373/i-need-to-display-a-certain-row-from-a-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!