如何在java的.txt文件中选择键及其适当的值

如何在java的.txt文件中选择键及其适当的值,java,hashmap,Java,Hashmap,这是我下面的.txt文件 我想根据URl检索IP地址。我尝试了以下代码,但失败了。我可以使用Hashmap吗。这是我的第一篇帖子。我为我的错误道歉 公共静态void main(字符串args[])引发IOException{ FileReader fr = new FileReader("C:/Users/charan/Desktop/Resources/HashCheck.txt"); BufferedReader br = new BufferedReader(fr);

这是我下面的.txt文件

我想根据URl检索IP地址。我尝试了以下代码,但失败了。我可以使用Hashmap吗。这是我的第一篇帖子。我为我的错误道歉

公共静态void main(字符串args[])引发IOException{

    FileReader fr = new FileReader("C:/Users/charan/Desktop/Resources/HashCheck.txt");
    BufferedReader br = new BufferedReader(fr);
    Scanner in = new Scanner(System.in);
    System.out.println("enter:");
    String output = in.next();
    String line;

    while((line =  br.readLine()) != null){
        if (line.contains(output))
        {
            output = line.split(":")[1].trim();
            System.out.println(output);
            break;
        }
        else
            System.out.println("UrL not found");
        break;
    }
    in.close();

}}

您想删除底部分隔符,否则它将在第一次迭代(第一行)时中断。如果您已对此进行调试,您可以很容易地看到它。@Nuthan98您的意思是在
else
周围有一个块
{…}
,该块内有
中断
?您有一个浮动的
}
关闭底部的支架end@Nic为什么会有帮助?这仍然意味着它将在第一次迭代时中断-哦,我明白了,你是说代码甚至没有编译?是的,有一个额外的结束brace@Nuthann92恐怖袋熊的建议解决了你的问题吗?
public static void main(String args[]) throws IOException {

    FileReader fr = new FileReader("C:/Users/charan/Desktop/Resources/HashCheck.txt");
    BufferedReader br = new BufferedReader(fr);
    Scanner in = new Scanner(System.in);
    System.out.println("enter:");
    String output = in.next();
    String line;
    String myIp = null;                       //assign a new variable to put IP into
    while ((line = br.readLine()) != null) {
        if (line.contains(output)) {
            myIp = line.split(":")[1].trim(); //assign IP to the new variable
            System.out.println(myIp);
            break;                            //break only if you get the IP. else let it iterate over all the elements.
        }
    }
    in.close();
    if (myIp == null) {                      //if the new variable is still null, IP is not found
        System.out.println("UrL not found");
    }
}