【问题标题】:Is there way to read a specific line in text file? [duplicate]有没有办法读取文本文件中的特定行? [复制]
【发布时间】:2019-07-15 07:20:31
【问题描述】:

我正在登录从 2 个文本文件读取的 gui。用户名行与密码中的行匹配。如果用户名在第 3 行,那么密码将在 password.txt 的第 3 行 如果用户名和密码在 username.txt 和 password.txt 中匹配,系统将让您登录。我可以从用户名文本,但在读取 password.txt 时我一直为空。

我尝试使用扫描仪,但我不需要阅读每一行。我只需要阅读特定的行。

private class ButtonHandler implements ActionListener  
    {
        public void actionPerformed (ActionEvent event) 
        {
            try {
                File f1=new File("username.txt");
                File f2=new File ("password.txt");
                String[] words=null; 
                FileReader fr1 = new FileReader(f1);
                FileReader fr2 = new FileReader(f2);  
                BufferedReader br1 = new BufferedReader(fr1);
                BufferedReader br2 = new BufferedReader(fr2);
                String s;
                String user = username.getText();
                String pass=String.valueOf(password.getPassword());
                String usertxt = " ";
                String passtxt=" ";
                int count =0;
                  while((s=br1.readLine())!=null)   //Reading Content from the file
                  {
                     words=s.split(" ");  //Split the word using space
                      for (String word : words) 
                      {
                             if (word.equals(user))   //Search for the given word
                             {

                                 System.out.print(count);

                                    for (int i = 0; i < count; i++)
                                    {
                                        br2.readLine();
                                        passtxt = br2.readLine();
                                    }
                                    System.out.print(passtxt);
                                    if(pass.equals(passtxt))
                                        JOptionPane.showMessageDialog(null,"User has logged in");                                           
                             }
                      }
                      count++;
                  }       
                }
            catch (IOException e) 
                {               
                System.out.println("Uh oh, got an IOException error!");
                e.printStackTrace();            
                }   
            }
        }

我希望字符串传递等于 passtxt。

【问题讨论】:

  • 你的函数的哪一部分是“我得到的都是空的”发生?你的意思是你得到一个 NullPointerException 吗?如果是这样,它发生在哪一行?

标签: java string user-interface readfile


【解决方案1】:

如果您知道该行从哪个字节位置开始,您可以使用 RandomAccessFile 并查找文件中的那个位置。

RandomAccessFile 方法 (seek) 的文档允许您将读取指针准确定位在您需要的位置。然后就可以使用read方法从文件中获取数据了。

但是,这可能对您不起作用,因为第三行几乎肯定会从您文件中的随机位置开始。随机位置是因为前两行的长度是可变的。

因此,除非您阅读文件以确定第 3 行的开始位置,否则随机访问在此问题中几乎没有用处。

如果您的文件是属性文件(例如)或其他结构(json 等)的形式,那么您始终可以使用库来帮助您读取文件的内容并提取所需的数据。

您可以参考Properties.load 方法以获取有关如何从文件加载属性的示例。

请注意,使用库访问文件可能会导致在库中读取整个文件。因此,如果您的目标是最小化 I/O 并且输入文件很大,您可能无法实现该目标。

恕我直言,最简单的方法是读取并丢弃第一行,直到到达所需的行,然后处理它们

【讨论】:

    猜你喜欢
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-04
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多