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