关于javashoir的信息

博主:adminadmin 2022-11-24 09:23:08 77

今天给各位分享javashoir的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

Java如何读取CPU的数据信息

java获取所有系统信息(CPU、内存、进程等)的代码:

import java.io.InputStreamReader;

import java.io.LineNumberReader;

import java.util.ArrayList;

import java.util.List;

import mytools.com.sun.management.OperatingSystemMXBean;

import mytools.java.io.File;

import mytools.java.lang.management.ManagementFactory;

/**

* 获取windows系统信息(CPU,内存,文件系统)

* @author libing

*

*/

public class WindowsInfoUtil {

private static final int CPUTIME = 500;

private static final int PERCENT = 100;

private static final int FAULTLENGTH = 10;

public static void main(String[] args) {

System.out.println(getCpuRatioForWindows());

System.out.println(getMemery());

System.out.println(getDisk());

}

//获取内存使用率

public static String getMemery(){

OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();

// 总的物理内存+虚拟内存

long totalvirtualMemory = osmxb.getTotalSwapSpaceSize();

// 剩余的物理内存

long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();

Double compare=(Double)(1-freePhysicalMemorySize*1.0/totalvirtualMemory)*100;

String str="内存已使用:"+compare.intValue()+"%";

return str;

}

//获取文件系统使用率

public static ListString getDisk() {

// 操作系统

ListString list=new ArrayListString();

for (char c = 'A'; c = 'Z'; c++) {

String dirName = c + ":/";

File win = new File(dirName);

if(win.exists()){

long total=(long)win.getTotalSpace();

long free=(long)win.getFreeSpace();

Double compare=(Double)(1-free*1.0/total)*100;

String str=c+":盘 已使用 "+compare.intValue()+"%";

list.add(str);

}

}

return list;

}

//获得cpu使用率

public static String getCpuRatioForWindows() {

try {

String procCmd = System.getenv("windir") + "\\system32\\wbem\\wmic.exe process get Caption,CommandLine,KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";

// 取进程信息

long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));

Thread.sleep(CPUTIME);

long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));

if (c0 != null c1 != null) {

long idletime = c1[0] - c0[0];

long busytime = c1[1] - c0[1];

return "CPU使用率:"+Double.valueOf(PERCENT * (busytime)*1.0 / (busytime + idletime)).intValue()+"%";

} else {

return "CPU使用率:"+0+"%";

}

} catch (Exception ex) {

ex.printStackTrace();

return "CPU使用率:"+0+"%";

}

}

//读取cpu相关信息

private static long[] readCpu(final Process proc) {

long[] retn = new long[2];

try {

proc.getOutputStream().close();

InputStreamReader ir = new InputStreamReader(proc.getInputStream());

LineNumberReader input = new LineNumberReader(ir);

String line = input.readLine();

if (line == null || line.length() FAULTLENGTH) {

return null;

}

int capidx = line.indexOf("Caption");

int cmdidx = line.indexOf("CommandLine");

int rocidx = line.indexOf("ReadOperationCount");

int umtidx = line.indexOf("UserModeTime");

int kmtidx = line.indexOf("KernelModeTime");

int wocidx = line.indexOf("WriteOperationCount");

long idletime = 0;

long kneltime = 0;

long usertime = 0;

while ((line = input.readLine()) != null) {

if (line.length() wocidx) {

continue;

}

// 字段出现顺序:Caption,CommandLine,KernelModeTime,ReadOperationCount,

// ThreadCount,UserModeTime,WriteOperation

String caption =substring(line, capidx, cmdidx - 1).trim();

String cmd = substring(line, cmdidx, kmtidx - 1).trim();

if (cmd.indexOf("wmic.exe") = 0) {

continue;

}

String s1 = substring(line, kmtidx, rocidx - 1).trim();

String s2 = substring(line, umtidx, wocidx - 1).trim();

if (caption.equals("System Idle Process") || caption.equals("System")) {

if (s1.length() 0)

idletime += Long.valueOf(s1).longValue();

if (s2.length() 0)

idletime += Long.valueOf(s2).longValue();

continue;

}

if (s1.length() 0)

kneltime += Long.valueOf(s1).longValue();

if (s2.length() 0)

usertime += Long.valueOf(s2).longValue();

}

retn[0] = idletime;

retn[1] = kneltime + usertime;

return retn;

} catch (Exception ex) {

ex.printStackTrace();

} finally {

try {

proc.getInputStream().close();

} catch (Exception e) {

e.printStackTrace();

}

}

return null;

}

/**

* 由于String.subString对汉字处理存在问题(把一个汉字视为一个字节),因此在 包含汉字的字符串时存在隐患,现调整如下:

* @param src 要截取的字符串

* @param start_idx 开始坐标(包括该坐标)

* @param end_idx 截止坐标(包括该坐标)

* @return

*/

private static String substring(String src, int start_idx, int end_idx) {

byte[] b = src.getBytes();

String tgt = "";

for (int i = start_idx; i = end_idx; i++) {

tgt += (char) b[i];

}

return tgt;

}

}

java 8种基础类型怎么使用?

| |--byte(1个字节,1字节=8个bit) 范围:-128 ~ 127\x0d\x0a | |--shoar(2个字节) 范围:-32768 ~ 32767\x0d\x0a |--4个整数:|--int(4个字节) 范围:-2147483648 ~ 2147483647\x0d\x0a | |--long(8个字节) 范围:-9223372036854775808 ~ 9223372036854775807\x0d\x0a | \x0d\x0a8种基本数据类型| |--float(4个字节) \x0d\x0a |--2个浮点类型:|--double(8个字节)\x0d\x0a | |---注意:浮点类型中默认为double类型,如果前面是0.的话可以写成.512\x0d\x0a | \x0d\x0a |--1个字节类型:|--char(2个字节) 一支持65536个字符。\x0d\x0a |\x0d\x0a |--1个特殊类型:|--boolean(1个字节) 值只有true 和 false。\x0d\x0a |

如何用java实现,实时获取局域网内所有接入设备的IP地址

1.得到局域网网段,可由自己机器的IP来确定 (也可以手动获取主机IP-CMD-ipconfig /all)

2.根据IP类型,一次遍历局域网内IP地址

JAVA类,编译之后直接运行便可以得到局域网内所有IP,具体怎样使用你自己编写相应代码调用便可

代码如下::

package bean;

import java.io.*;

import java.util.*;

public class Ip{

static public HashMap ping; //ping 后的结果集

public HashMap getPing(){ //用来得到ping后的结果集

return ping;

}

//当前线程的数量, 防止过多线程摧毁电脑

static int threadCount = 0;

public Ip() {

ping = new HashMap();

}

public void Ping(String ip) throws Exception{

//最多30个线程

while(threadCount30)

Thread.sleep(50);

threadCount +=1;

PingIp p = new PingIp(ip);

p.start();

}

public void PingAll() throws Exception{

//首先得到本机的IP,得到网段

InetAddress host = InetAddress.getLocalHost();

String hostAddress = host.getHostAddress();

int k=0;

k=hostAddress.lastIndexOf(".");

String ss = hostAddress.substring(0,k+1);

for(int i=1;i =255;i++){ //对所有局域网Ip

String iip=ss+i;

Ping(iip);

}

//等着所有Ping结束

while(threadCount0)

Thread.sleep(50);

}

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

Ip ip= new Ip();

ip.PingAll();

java.util.Set entries = ping.entrySet();

Iterator iter=entries.iterator();

String k;

while(iter.hasNext()){

Map.Entry entry=(Map.Entry)iter.next();

String key=(String)entry.getKey();

String value=(String)entry.getValue();

if(value.equals("true"))

System.out.println(key+"--"+value);

}

}

class PingIp extends Thread{

public String ip; // IP

public PingIp(String ip){

this.ip=ip;

}

public void run(){

try{

Process p= Runtime.getRuntime().exec ("ping "+ip+ " -w 300 -n 1");

InputStreamReader ir = new InputStreamReader(p.getInputStream());

LineNumberReader input = new LineNumberReader (ir);

//读取结果行

for (int i=1 ; i 7; i++)

input.readLine();

String line= input.readLine();

if (line.length() 17 || line.substring(8,17).equals("timed out"))

ping.put(ip,"false");

else

ping.put(ip,"true");

//线程结束

threadCount -= 1;

}catch (IOException e){}

}

}

}

有没有java 开源的简单搜索引擎

被邀请回答这个问题,但是似乎问错人了,问的是开源的搜索引擎,但是很抱歉,我对此领域并不熟悉。

我所知道的搜索引擎方案仅有:

1. Nutch ()

2. Datapark search

以下列表不是一个完整的搜索引擎方案,一般都不包括爬虫,确切的说是IR或者全文检索引擎更为恰当:

1. Zettair

2. Indri

3. Terrier  

4. Galago

5. Zebra

6. Lucene 及其变种

Lucene

Solr

Elasticsearch  

7. Sphinx

8. Xiapian

java中怎样用post,get,put请求

java中用post,get,put请求方法:

public static String javaHttpGet(String url,String charSet){

String resultData = null;

try {

URL pathUrl = new URL(url); //创建一个URL对象

HttpURLConnection urlConnect = (HttpURLConnection) pathUrl.openConnection(); //打开一个HttpURLConnection连接

urlConnect.setConnectTimeout(30000); // 设置连接超时时间

urlConnect.connect();

if (urlConnect.getResponseCode() == 200) { //请求成功

resultData = readInputStream(urlConnect.getInputStream(), charSet);

}

} catch (MalformedURLException e) {

LogL.getInstance().getLog().error("URL出错!", e);

} catch (IOException e) {

LogL.getInstance().getLog().error("读取数据流出错!", e);

}

return resultData;

}

public static String javaHttpPost(String url,MapString,Object map,String charSet){

String resultData=null;

StringBuffer params = new StringBuffer();

try {

IteratorEntryString, Object ir = map.entrySet().iterator();

while (ir.hasNext()) {

Map.EntryString, Object entry = (Map.EntryString, Object) ir.next();

params.append(URLEncoder.encode(entry.getKey(),charSet) + "=" + URLEncoder.encode(entry.getValue().toString(), charSet) + "");

}

byte[] postData = params.deleteCharAt(params.length()).toString().getBytes();

URL pathUrl = new URL(url); //创建一个URL对象

HttpURLConnection urlConnect = (HttpURLConnection) pathUrl.openConnection();

urlConnect.setConnectTimeout(30000); // 设置连接超时时间

urlConnect.setDoOutput(true); //post请求必须设置允许输出

urlConnect.setUseCaches(false); //post请求不能使用缓存

urlConnect.setRequestMethod("POST"); //设置post方式请求

urlConnect.setInstanceFollowRedirects(true);

urlConnect.setRequestProperty("Content-Type","application/x-www-form-urlencoded; charset="+charSet);// 配置请求Content-Type

urlConnect.connect(); // 开始连接

DataOutputStream dos = new DataOutputStream(urlConnect.getOutputStream()); // 发送请求参数

dos.write(postData);

dos.flush();

dos.close();

if (urlConnect.getResponseCode() == 200) { //请求成功

resultData = readInputStream(urlConnect.getInputStream(),charSet);

}

} catch (MalformedURLException e) {

LogL.getInstance().getLog().error("URL出错!", e);

} catch (IOException e) {

LogL.getInstance().getLog().error("读取数据流出错!", e);

} catch (Exception e) {

LogL.getInstance().getLog().error("POST出错!", e);

}

return resultData;

}

关于javashoir和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-24,除非注明,否则均为首码项目网原创文章,转载请注明出处。