集册 Java实例教程 返回本地主机的主机名。

返回本地主机的主机名。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

444
返回本地主机的主机名。

/*

 * Copyright (c) 2015-2016 QuartzDesk.com.

 * Licensed under the MIT license (https://opensource.org/licenses/MIT).

 */

import java.io.BufferedInputStream;
/** 
来 自 
n o w j a v a . c o m - 时  代  Java
**/

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.io.Reader;

import java.io.StringWriter;

import java.lang.reflect.Array;

import java.net.InetAddress;

import java.net.UnknownHostException;

import java.sql.SQLException;

import java.util.Arrays;


public class Main{

    public static void main(String[] argv) throws Exception{

        System.out.println(getLocalHost());

    }

    /**

     * Returns the hostname of the local host. If the host name

     * cannot be determined, this method returns an empty string.

     *

     * @return the hostname of the local host.

     */

    public static String getLocalHost() {

        try {
        /* from 
        N o  w  J a v a . c o m - 时  代  Java*/

       
展开阅读全文