kotlin獲取屬性_Kotlin程序獲取系統MAC地址

kotlin獲取屬性

The task is to get system MAC address.

任務是獲取系統MAC地址。

package com.includehelp
import java.net.InetAddress
import java.net.NetworkInterface
//Function to get System MAC
fun getSystemMac(): String? {
return try {
val OSName = System.getProperty("os.name")     //Get Operating System Name
if (OSName.contains("Windows")) {
getMAC4Windows()                           // Call if OS is Windows
} else {
var mac = getMAC4Linux("eth0")
if (mac == null) {
mac = getMAC4Linux("eth1")
if (mac == null) {
mac = getMAC4Linux("eth2")
if (mac == null) {
mac = getMAC4Linux("usb0")
}
}
}
mac
}
} catch (E: Exception) {
System.err.println("System Mac Exp : " + E.message)
null
}
}
/**
* Method for get MAc of Linux Machine
*/
fun getMAC4Linux(name: String): String? {
return try {
val network = NetworkInterface.getByName(name)
val mac = network.hardwareAddress
val sb = StringBuilder()
for (i in mac.indices) {
sb.append(String.format("%02X%s", mac[i], if (i < mac.size - 1) "-" else ""))
}
sb.toString()
} catch (E: Exception) {
System.err.println("System Linux MAC Exp : " + E.message)
null
}
}
/**
* Method for get Mac Address of Windows Machine
*/
fun getMAC4Windows(): String? {
return try {
val addr = InetAddress.getLocalHost()
val network = NetworkInterface.getByInetAddress(addr)
val mac = network.hardwareAddress
val sb = StringBuilder()
for (i in mac.indices) {
sb.append(String.format("%02X%s", mac[i], if (i < mac.size - 1) "-" else ""))
}
sb.toString()
} catch (E: Exception) {
System.err.println("System Windows MAC Exp : " + E.message)
null
}
}
//Main Function, Entry Point of Program
fun main(args: Array<String>) {
//Call Function to get MAC Address
val macAddress = getSystemMac()
//Print MAC Address
println("System Mac Address : $macAddress")
}

Output

輸出量

System Mac Address : 02-12-FE-11-00-05

翻譯自: https://www.includehelp.com/kotlin/get-system-mac-address.aspx

kotlin獲取屬性

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377724.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377724.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377724.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

帶分頁功能的SSH整合,DAO層經典封裝

任何一個封裝講究的是&#xff0c;使用&#xff0c;多狀態。Action&#xff1a;任何一個Action繼承分頁有關參數類PageManage&#xff0c;自然考慮的到分頁效果&#xff0c;我們必須定義下幾個分頁的參數。并根據這個參數進行查值。然后在繼承ServiceManage&#xff0c;Service…

在windows phone Mango中使用原生代碼開發程序

本文不討論創建可執行的exe程序,主要想說明怎么在silverlight程序里面調用由原生代碼所編寫的DLL(C / ARM). 原生代碼可以調用更多的API,但是這并不是說你就能隨意獲得那些你沒有權限的資源,比如,你可以使用CopyFile這個API,但是如果你試圖把文件Copy到\Windows文件夾,就會得到…

leetcode 198. 打家劫舍 思考分析

目錄1、題目2、求解思路3、代碼1、題目 你是一個專業的小偷&#xff0c;計劃偷竊沿街的房屋。每間房內都藏有一定的現金&#xff0c;影響你偷竊的唯一制約因素就是相鄰的房屋裝有相互連通的防盜系統&#xff0c;如果兩間相鄰的房屋在同一晚上被小偷闖入&#xff0c;系統會自動…

找不到Windows照片查看器解決方法

桌面創建一個txt文本 復制這些命令&#xff0c;之后將后綴改為.reg&#xff0c;右擊管理員身份運行即可 Windows Registry Editor Version 5.00 ; Change Extensions File Type [HKEY_CURRENT_USER\Software\Classes\.jpg] "PhotoViewer.FileAssoc.Tiff" ; Change E…

數字拆分為斐波那契數列_檢查數字是否為斐波那契

數字拆分為斐波那契數列Description: 描述&#xff1a; We are often used to generate Fibonacci numbers. But in this article, we are going to learn about how to search Fibonacci numbers in an array? 我們經常被用來產生斐波那契數。 但是在本文中&#xff0c;我們…

伙伴分配器的一個極簡實現

提起buddy system相信很多人不會陌生&#xff0c;它是一種經典的內存分配算法&#xff0c;大名鼎鼎的Linux底層的內存管理用的就是它。這里不探討內核這么復雜實現&#xff0c;而僅僅是將該算法抽象提取出來&#xff0c;同時給出一份及其簡潔的源碼實現&#xff0c;以便定制擴展…

[USACO3.2.3 Spinning Wheels]

[關鍵字]&#xff1a;模擬 枚舉 [題目大意]&#xff1a;有5個輪子&#xff0c;每個輪子優r個缺口并且會按一定速度不停轉動&#xff0c;問什么時候可以使一條光線射過所有輪子。 // [分析]&#xff1a;從0到1000&#xff08;或其他的&#xff09;枚舉分鐘然后判斷&#xff0c;當…

一、SQLServer2008安裝(帶密碼)、創建數據庫、C#窗體項目測試

一、下載和安裝SQLServer2008 東西太大了&#xff0c;沒法上傳到資源里面&#xff0c;官網其他公眾號都下載可以。 右擊管理員身份 運行setup.exe 這個密鑰不能用的話&#xff0c;也可以去百度其他密鑰 JD8Y6-HQG69-P9H84-XDTPG-34MBB 建議改一下路徑&#xff0c;我這邊修…

python獲取當前日期_Python程序獲取當前日期

python獲取當前日期In the below example – we are implementing a python program to get the current date. 在下面的示例中-我們正在實現一個python程序來獲取當前日期 。 Steps: 腳步&#xff1a; Import the date class from datetime module. 從datetime模塊導入日期類…

【C++grammar】多態、聯編、虛函數

目錄1、多態概念1.多態性有兩種表現的方式2、聯編&#xff08;實現多態&#xff09;1.靜態聯編2.動態聯編3、實現運行時多態1.為何要使用運行時多態&#xff1f;2.如何實現運行時多態3.多態的例子1.調用哪個同名虛函數&#xff1f;2. 用途&#xff1a;可以用父類指針訪問子類對…

一 MVC - HtmlHelper

HtmlHelper類位于System.Web.Mvc.Html之中主要有七個靜態類組成&#xff1a; FormExtensions - BeginForm, BeginRouteForm, EndForm InputExtensions - CheckBox, CheckBoxFor, Hidden, HiddenFor, Password, PasswordFor, RadioButton, RadioButtonFor, TextBox, TextBoxFor …

HDOJ 400題紀念。

剛剛交了1506&#xff0c;無意間瞟到左邊的隨筆數&#xff0c;發現已經401題了&#xff0c;這么說前幾天就400題了啊囧。 昨天還想交到400題就先放放&#xff0c;背單詞的&#xff0c;沒想到那么快。等把USACO那個八皇后寫完吧。人生總是有許多不想做又不得不做的事情。。。 還…

二、用戶登錄和注冊

一、頁面設計 一共四個頁面 主頁面Form1&#xff0c;登錄頁面login&#xff0c;注冊頁面resister&#xff0c;主菜單頁面main_page 系統運行進入Form1&#xff0c;單擊登錄按鈕跳轉到login&#xff0c;數據庫中得存在數據信息且輸入正確才可登錄成功&#xff0c;跳轉到main_pa…

readdir函數_PHP readdir()函數與示例

readdir函數PHP readdir()函數 (PHP readdir() function) The full form of readdir is "Read Directory", the function readdir() is used to read the directory i.e. read the name of the next entry in the directory. readdir的完整形式為“ Read Directory”…

【C++grammar】訪問控制與抽象類與純虛函數

目錄一、訪問控制 (可見性控制)1.private、public、protected關鍵字2.關鍵字示例1、關鍵字對類數據成員訪問的限制3. 公有繼承4. 私有繼承5. 保護繼承6. 私有繼承和保護繼承的區別二、抽象類與純虛函數1.什么是抽象類2.抽象函數/純虛函數3.抽象類示例一、訪問控制 (可見性控制)…

mongodb 如何刪除 字段值為 json對象中的某個字段值

例如&#xff1a; { attributes: { birthday:1988-01-01, name: aq } } birthday是attributes字段的value的一個字段&#xff0c; 我要刪除birthday 用這句話&#xff1a; db.User.update({email:adminlinkris.com},{$unset:{attributes.birthday:}})轉載于:https://www.cnblog…

使用 Spring 的 Web 服務模擬器框架解決方案

http://www.ibm.com/developerworks/cn/web/wa-aj-simulator/index.html轉載于:https://www.cnblogs.com/diyunpeng/archive/2012/02/28/2371390.html

三、上傳織物圖片至SQL Server并提供name進行展示織物照片

一、數據庫的建立 還是在fiber_yy數據庫下創建images表 images表設計如下 二、頁面完善設計 main_page頁面進行功能完善 入庫管理系統 warehousing頁面 庫存查詢系統 query頁面 登錄注冊頁面前面幾個博文已經實現過了&#xff0c;這里就再贅述了&#xff0c;仍是沿用前…

gettype_PHP gettype()函數與示例

gettypePHP gettype()函數 (PHP gettype() function) In PHP, we have a library function gettype() to identify the type of data. The function is primarily used to sanity check the type of data being input in a variable. The function can identify the data into …

ARM MMU工作原理剖析[轉]

一、MMU的產生 許多年以前&#xff0c;當人們還在使用DOS或是更古老的操作系統的時候&#xff0c;計算機的內存還非常小&#xff0c;一般都是以K為單位進行計算&#xff0c;相應的&#xff0c;當時的程序規模也不大&#xff0c;所以內存容量雖然小&#xff0c;但還是可以容納當…