All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ip2location.Country Maven / Gradle / Ivy

Go to download

IP2Location Java Component enables applications to get info from IP address such as the visitor’s country, region, city, latitude, longitude, ZIP code, ISP name, domain name, time zone, connection speed, IDD code, area code, weather station code, weather station name, MCC, MNC, mobile brand name, elevation, usage type, address type, IAB category, district, autonomous system number (ASN) and autonomous system (AS).

The newest version!
package com.ip2location;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.File;
import com.opencsv.*;
import com.opencsv.exceptions.*;

/**
 * This class parses country information CSV and returns the country information.
 * 

* Copyright (c) 2002-2024 IP2Location.com *

* * @author IP2Location.com * @version 8.12.0 */ public class Country { private final Map> records = new HashMap<>(); /** * This constructor reads the country information CSV and store the parsed info. * * @param CSVFile The full path to the country information CSV file. */ public Country(String CSVFile) throws IOException, CsvValidationException { Map line; File file = new File(CSVFile); if (!file.exists()) { throw new IOException("The CSV file '" + CSVFile + "' is not found."); } FileReader fr = new FileReader(file); if (fr.read() == -1) { throw new IOException("Unable to read '" + CSVFile + "'."); } CSVReaderHeaderAware reader = new CSVReaderHeaderAware(new FileReader(file)); while ((line = reader.readMap()) != null) { if (line.containsKey("country_code")) { records.put(line.get("country_code"), line); } else { throw new IOException("Invalid country information CSV file."); } } } /** * This function gets the country information for the supplied country code. * * @param CountryCode ISO-3166 country code * @return Map */ public Map GetCountryInfo(String CountryCode) throws IOException { if (records.isEmpty()) { throw new IOException("No record available."); } else { return records.getOrDefault(CountryCode, null); } } /** * This function gets the country information for all countries. * * @return List */ public List> GetCountryInfo() throws IOException { List> results = new ArrayList<>(); if (records.isEmpty()) { throw new IOException("No record available."); } else { for (Map.Entry> entry : records.entrySet()) { results.add(entry.getValue()); } } return results; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy