This question has already been answered:

Wrote code for example from my application. The bottom line is this. There is a text document with IPs and names. The application reads it and puts it in the HashMap. Problem: The ipishnik that is in the first line with the containsKey method does not find it. I go through all the keys of Mar - he really is. It is the first line. Totzhe text put in the second line - all good.

import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashMap; public class MapTesting { HashMap<String, String> mapallowedClients; private void readAllowedClients(){ mapallowedClients = new HashMap<>(); try { InputStream in = getClass().getResourceAsStream("/allowedClients.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF8")); while(reader.ready()){ String[] client = reader.readLine().split(":"); if(client.length>1){ mapallowedClients.put(client[0],client[1]); } } in.close(); reader.close(); } catch(Exception e){ e.printStackTrace(); } } public static void main(String[] args) { MapTesting mp = new MapTesting(); mp.readAllowedClients(); System.out.println(mp.mapallowedClients.containsKey("10.244.1.121")); } } 

Here is a list of a text document with IPs:

 10.244.1.121:sadas 10.244.1.120:sadas2 10.244.1.10:Опер 10.244.1.22:Гонтар 10.244.1.23:КПП2 10.244.1.31:КПП1 10.244.1.34:Дроздовський 10.244.1.69:Сагін 10.244.1.120:Опер120 10.244.1.169:Дідик 10.244.1.189:Гідзула 10.244.1.232:П-010 10.244.1.16:СПС 127.0.0.1:Авдонін 192.168.0.102:Clebo 192.168.0.100:Home 

Reported as a duplicate member of Spirit Community Jun 26 '18 at 12:54 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    How I came to the solution of the problem .... At first, the information from the first line saved the variable in the string. Then he compared this variable with "10.244.1.121". Again false. Then I split the resulting variable into a char array. And as it turned out in the first line when calling the char []. Lenght method for one element more than 10.244.1.121. But it is empty and not displayed at all. Then I went here and read the comment about the duplicate question. As it turned out, from the very beginning of the file with UTF encoding is written BOM !!!! Wow ...... and he patted my nerves. Hivemaster, Thanks for the comment! As a result, wrote like this:

     String[] client = reader.readLine().replace("\uFEFF", "").split(":");