Skip to content

Instantly share code, notes, and snippets.

@fty4
Created May 4, 2017 13:44
Show Gist options
  • Save fty4/243a3738a262bc20a38955948af3d91b to your computer and use it in GitHub Desktop.
Save fty4/243a3738a262bc20a38955948af3d91b to your computer and use it in GitHub Desktop.
Reads hostnames from file and searches for their IP-addresses
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
public class getIpFromHostname {
public static void main(String[] args) {
InetAddress address;
try {
FileReader fr;
FileWriter fw;
BufferedReader br;
PrintWriter pw;
fr = new FileReader("C:\\Marco\\tmp\\hostnames.txt");
br = new BufferedReader(fr);
String strText;
String iplist = "";
while ((strText = br.readLine()) != null)
{
address = InetAddress.getByName(strText);
iplist += address.getHostAddress() + "\r\n";
System.out.println(address.getHostAddress());
}
br.close();
fr.close();
fw = new FileWriter("C:\\Marco\\tmp\\ips.txt", false);
pw = new PrintWriter(fw);
pw.write(iplist);
pw.close();
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment