1. 程式人生 > >Java程式設計實現獲取本機IP和計算機名的功能

Java程式設計實現獲取本機IP和計算機名的功能

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Test {

	public static void main(String[] args) {
		try {
			InetAddress inetAddress = InetAddress.getLocalHost();
			System.out.println("本機IP:"	+ inetAddress.getHostAddress().toString());
			System.out.println("本機名稱:" + inetAddress.getHostName().toString());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
}