
正文
java代码硬盘使用比例 java代码硬盘使用比例是多少
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java程序 做一个这样的程序:统计某磁盘的使用情况 统计在这磁盘中,各种文件的个数和占磁盘空间
import java.io.File;
import java.io.FileFilter;
import java.util.HashMap;
import java.util.Map;
public class T1 {
/**存放文件后缀 对应的大小*/
private static final MapString, Long sizeMap = new HashMapString, Long();
/**存放文件后缀 对应的个数*/
private static final MapString, Integer countMap = new HashMapString, Integer();
public static void main(String[] args) {
String path = "F:";
filter(path);
for(String key :sizeMap.keySet()) {
System.out.println("后缀:" + key + "\t字节:" + (sizeMap.get(key)==null?0:sizeMap.get(key)) + "\t个数为" + (countMap.get(key)==null?0:countMap.get(key)));
}
}
public static void filter(String path){
File file = new File(path);
file.listFiles(new FileFilter() {
@Override
public boolean accept(File f) {
if(f.isDirectory()) {
filter(f.getPath());
return false;
}
String fileName = f.getName();
if(fileName.indexOf(".") == -1) {
return false;
}
String suffix =fileName.split("\\.")[1];//获得文件后缀
//把文件后缀相同的字节数相加
Long size = (sizeMap.get(suffix)==null?0:sizeMap.get(suffix)) + f.length();
sizeMap.put(suffix, size);
//把文件后缀相同的个数相加
Integer count = (countMap.get(suffix)==null?0:countMap.get(suffix)) + 1;
countMap.put(suffix, count);
return false;
}
});
}
}
运行结果太长了,我随便截取点吧:
后缀:1-1 字节:1820 个数为2
后缀:dll_2016-05-23_000 字节:345 个数为1
后缀:dat 字节:20253796 个数为28
后缀:1-2 字节:1302 个数为2
后缀:md 字节:53548 个数为4
后缀:MF 字节:1105 个数为10
后缀:html 字节:745985 个数为75
后缀:lrc 字节:6872 个数为6
后缀:9-2 字节:1478 个数为2
后缀:dll_2016-03-31_000 字节:55 个数为1
后缀:9-3 字节:1436 个数为2
后缀:all 字节:3003366 个数为4
后缀:66b 字节:6786365 个数为1
后缀:withoutimage 字节:383536 个数为4
后缀:eclipse 字节:27666 个数为104
后缀:woff 字节:437588 个数为20
后缀:spr 字节:15084930 个数为311
后缀:mdl 字节:79521996 个数为487
后缀:m3d 字节:199680 个数为3
后缀:JPG 字节:261216 个数为113
后缀:fgd 字节:37819 个数为1
后缀:79d 字节:23795338 个数为3
后缀:79e 字节:8001129 个数为1
后缀:gif 字节:1453085 个数为597
后缀:dll_2016-04-26_000 字节:115 个数为1
后缀:70LeagueV 字节:549361 个数为1
后缀:lst 字节:47492 个数为13
后缀:26q_v1 字节:7284946 个数为1
后缀:sql 字节:87909 个数为6
后缀:11-4 字节:1230 个数为2
后缀:11-3 字节:1804 个数为2
后缀:timer 字节:10452 个数为3
后缀:html5only 字节:467448 个数为4
后缀:dll_2016-05-17_000 字节:115 个数为1
后缀:11-2 字节:1770 个数为2
后缀:11-1 字节:2032 个数为2
后缀:flexslider-min 字节:65025 个数为3
后缀:dll_2016-07-01_000 字节:13427 个数为2
后缀:greenxf 字节:97562 个数为1
特地敲了代码 要采纳啊,我这测试的是F盘 是可以的, 你有什么疑问 可追问我
相关问答
Q1: java程序员500g移动硬盘够用吗
java程序员500g移动硬盘不够用,因为作为程序员的话,所要配备的程序是比较多的,而且书籍方面以及教程方面也是非常大的,所以500G移动硬盘是不够用的。
Q2: java怎样获取CPU占用率和硬盘占用率
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class T {
private static final int DRIVE_TYPE_HARD = 3;
private static final String CHARSET = "GBK";
private static final String CAP_LOAD_PERCENTAGE = "LoadPercentage";
private static final String CAP_CAPACITY = "Capacity";
private static final String CAP_CAPTION = "Caption";
private static final String CAP_DRIVE_LETTER = "DriveLetter";
private static final String CAP_DRIVE_TYPE = "DriveType";
private static final String CAP_FREE_SPACE = "FreeSpace";
private static final ListString CAPS_VOLUME = Arrays.asList(CAP_CAPACITY,
CAP_CAPTION, CAP_DRIVE_LETTER, CAP_DRIVE_TYPE, CAP_FREE_SPACE);
private static final String CMD_CPU = "wmic cpu get " + CAP_LOAD_PERCENTAGE;
private static final String CMD_VOLUME = "wmic volume get " + CAP_CAPACITY
+ "," + CAP_CAPTION + "," + CAP_DRIVE_LETTER + "," + CAP_DRIVE_TYPE + ","
+ CAP_FREE_SPACE;
public static void main(String[] args) throws IOException {
printDiskUsages(getDiskUsages());
printCpuUsage(getCpuLoadPercentage());
}
private static void printDiskUsages(ListDiskUsage diskUsages) {
for (DiskUsage diskUsage : diskUsages) {
System.out.printf("%s占用率java代码硬盘使用比例:%.2f%%\n", diskUsage.caption, diskUsage.usage);
}
}
private static void printCpuUsage(double cpuLoadPercentage) {
System.out.printf("CPU占用率java代码硬盘使用比例:%.2f%%\n", cpuLoadPercentage);
}
/**
* 取得 CPU 占用率。
*
* @return
* @throws IOException
*/
private static double getCpuLoadPercentage() throws IOException {
Process process = Runtime.getRuntime().exec(CMD_CPU);
InputStream is = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, CHARSET));
br.readLine(); // 舍弃标题行
br.readLine(); // 舍弃标题行下空行
String percentageLine = br.readLine();
if (percentageLine == null) {
return 0;
}
return Double.parseDouble(percentageLine.trim());
}
/**
* 取得硬盘占用率。
*
* @return
* @throws IOException
*/
private static ListDiskUsage getDiskUsages() throws IOException {
Process process = Runtime.getRuntime().exec(CMD_VOLUME);
InputStream is = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, CHARSET));
String captionLine = br.readLine(); // 舍弃标题行
br.readLine(); // 舍弃标题行下空行
MapString, Integer captionToIndex = parseVolumeCaptionLine(captionLine);
String line;
ListDiskUsage result = new ArrayListDiskUsage();
while ((line = br.readLine()) != null) {
if (line.length() == 0) {
// 舍弃空行
continue;
}
DiskUsage diskUsage = parseVolumeLine(line, captionToIndex);
if (diskUsage != null) {
result.add(diskUsage);
}
}
Collections.sort(result, new DiskUsageComparator());
return result;
}
private static MapString, Integer parseVolumeCaptionLine(String captionLine) {
MapString, Integer captionToIndex = new HashMapString, Integer();
for (String caption : CAPS_VOLUME) {
captionToIndex.put(caption, captionLine.indexOf(caption));
}
return captionToIndex;
}
private static DiskUsage parseVolumeLine(String line,
MapString, Integer captionToIndex) {
int driveType = parseVolumeDriveType(line, captionToIndex);
if (driveType != DRIVE_TYPE_HARD) {
return null;
}
String driveLetter = parseVolumeDriveLetter(line, captionToIndex);
if (!isValidDriveLetter(driveLetter)) {
return null;
}
String caption = parseVolumeCaption(line, captionToIndex);
long capacity = parseVolumeCapacity(line, captionToIndex);
long freeSpace = parseVolumeFreeSpace(line, captionToIndex);
return new DiskUsage(caption,
((capacity - freeSpace) / (double) capacity) * 100);
}
private static boolean isValidDriveLetter(String driveLetter) {
if (driveLetter.length() != 2) {
return false;
}
return Character.isUpperCase(driveLetter.charAt(0));
}
private static int parseVolumeDriveType(String line,
MapString, Integer captionToIndex) {
String str = line.substring(captionToIndex.get(CAP_DRIVE_TYPE),
captionToIndex.get(CAP_FREE_SPACE));
return Integer.parseInt(str.trim());
}
private static String parseVolumeDriveLetter(String line,
MapString, Integer captionToIndex) {
String str = line.substring(captionToIndex.get(CAP_DRIVE_LETTER),
captionToIndex.get(CAP_DRIVE_TYPE));
return str.trim();
}
private static String parseVolumeCaption(String line,
MapString, Integer captionToIndex) {
String str = line.substring(captionToIndex.get(CAP_CAPTION),
captionToIndex.get(CAP_DRIVE_LETTER));
return str.trim();
}
private static long parseVolumeCapacity(String line,
MapString, Integer captionToIndex) {
String str = line.substring(captionToIndex.get(CAP_CAPACITY),
captionToIndex.get(CAP_CAPTION));
return Long.parseLong(str.trim());
}
private static long parseVolumeFreeSpace(String line,
MapString, Integer captionToIndex) {
String str = line.substring(captionToIndex.get(CAP_FREE_SPACE));
return Long.parseLong(str.trim());
}
private static class DiskUsageComparator implements ComparatorDiskUsage {
@Override
public int compare(DiskUsage o1, DiskUsage o2) {
return o1.caption.compareTo(o2.caption);
}
}
private static class DiskUsage {
public String caption;
public double usage;
public DiskUsage(String caption, Double usage) {
this.caption = caption;
this.usage = usage;
}
}
}
Q3: Java获取服务器磁盘负载率
1、在远程server里搭建一个http服务java代码硬盘使用比例,并支持某种服务端语言(如.net或php之类)
2、用服务端语言获取到服务器的磁盘信息java代码硬盘使用比例,把结果输出
3、在本地用JS写一段AJAX脚本,远程请求服务端写好的程序,得到结果,并展示给用户
html
head
title提取硬盘序列号/title
script
function disk() {//硬盘序列号 信息
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_DiskDrive");
var e = new Enumerator (properties);
document.write("table border=1");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("tr");
document.write("td" + p.signature + "/td");
document.write("/tr");
}
document.write("/table");
}
/script
/head
body
input type="button" value="硬盘序列号" onclick="disk()"
/body
/html
在这上找的代码:
对于写过ASP或.net 的人来说,通过asp或.net 语言来获得客户端的硬件信息是很熟悉的.但如何通过javascript这种客户端脚本来获得客户端的信息呢?请看以下实例:
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
html
head
titlejavascript获得客户端硬件信息-;/title
meta name="GENERATOR" content="Microsoft Visual Studio .NET
7.1"
meta name="vs_targetSchema"
content=""
script id=clientEventHandlersJS language=javascript
!--
function Button2_onclick() {//CPU 信息
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_Processor");
var e = new Enumerator (properties);
document.write("table border=1");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("tr");
document.write("td" + p.Caption + "/td");
document.write("td" + p.DeviceID + "/td");
document.write("td" + p.Name + "/td");
document.write("td" + p.CpuStatus + "/td");
document.write("td" + p.Availability + "/td");
document.write("td" + p.Level + "/td");
document.write("td" + p.ProcessorID + "/td");
document.write("td" + p.SystemName + "/td");
document.write("td" + p.ProcessorType + "/td");
document.write("/tr");
}
document.write("/table");
}
function Button1_onclick() {//软盘信息
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_FloppyDrive");
var e = new Enumerator (properties);
document.write("table border=1");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("tr");
document.write("td" + p.Description + "/td");
document.write("td" + p.DeviceID + "/td");
document.write("td" + p.Status + "/td");
document.write("td" + p.Manufacuturer + "/td");
document.write("/tr");
}
document.write("/table");
}
function Button1_onclick() {//CD-ROM 信息
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_CDROMDrive");
var e = new Enumerator (properties);
document.write("table border=1");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("tr");
document.write("td" + p.Caption + "/td");
document.write("td" + p.Description + "/td");
document.write("td" + p.Drive + "/td");
document.write("td" + p.Status + "/td");
document.write("td" + p.MediaLoaded + "/td");
document.write("/tr");
}
document.write("/table");
}
function Button1_onclick() {//键盘信息
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_Keyboard");
var e = new Enumerator (properties);
document.write("table border=1");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("tr");
document.write("td" + p.Description + "/td");
document.write("td" + p.Name + "/td");
document.write("td" + p.Status + "/td");
document.write("/tr");
}
document.write("/table");
}
function Button1_onclick() {//主板信息
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_BaseBoard");
var e = new Enumerator (properties);
document.write("table border=1");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("tr");
document.write("td" + p.HostingBoard + "/td");
document.write("td" + p.Manufacturer + "/td");
document.write("td" + p.PoweredOn + "/td");
document.write("td" + p.Product + "/td");
document.write("td" + p.SerialNumber + "/td");
document.write("td" + p.Version + "/td");
document.write("/tr");
}
document.write("/table");
}
//--
/script
/head
body
INPUT id="Button1" type="button" value="Button"
name="Button1" language=javascript onclick="return Button1_onclick()"
/body
/html
使用javascript方式获得客户端的信息主要的优点是,不需求服务器资源.不需求刷新网页.直接在客户端执行脚本获得显示.
在这上找到了硬盘序列号的参数:
\'获得硬盘序列号
Dim cmicWmi As New System.Management.ManagementObjectSearcher(\"SELECT * FROM Win32_DiskDrive\")
Dim Uint32 As UInt32
For Each cmicWmiObj As ManagementObject In cmicWmi.Get
Uint32 = cmicWmiObj(\"signature\")
Next
TextBox1.Text = Uint32.ToString
Q4: Java开发硬盘分区166G够不
硬盘区分166G够用了。
java内存用166G是足够的,1G是1024mb,166G就是169984mb,如果需要装的东西不是特别多足够使用的,不够使用可以扩充。
Java是一门面向对象的编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。
Q5: Java获得内存使用,磁盘情况
import java io File;
/**
*
* jdk 下的磁盘使用情况例子
*/
public class Diskfree {
public static void main(String[] args) {
File[] roots = File listRoots();//获取磁盘分区列表
for (File file : roots) {
System out println(file getPath()+ 信息如下: );
System out println( 空闲未使用 = + file getFreeSpace()/ / / + G );//空闲空间
System out println( 已经使用 = + file getUsableSpace()/ / / + G );//可用空间
System out println( 总容量 = + file getTotalSpace()/ / / + G );//总空间
System out println();
}
}
}
java获得当前系统内存情况的代码如下
Java code
import java lang management ManagementFactory;
import sun management OperatingSystemMXBean;
public class OSTest {
public static void main(String[] args)
{
OperatingSystemMXBean o *** b = (OperatingSystemMXBean) ManagementFactory getOperatingSystemMXBean();
System out println( 系统物理内存总计 + o *** b getTotalPhysicalMemorySize() / / + MB );
System out println( 系统物理可用内存总计 + o *** b getFreePhysicalMemorySize() / / + MB ); }
}
ManagementFactory getOperatingSystemMXBean()返回的是java lang management里面的OperatingSystemMXBean
我们要用的是 sun management OperatingSystemMXBean;
在java类库中可以查到
public abstract Interface sun management OperatingSystemMXBean extends java lang management OperatingSystemMXBean
所以我们可以强制转换一下
想得到磁盘使用情况的话 可以看 楼的例子 要得到系统信息的话 使用System getProperty(String key);
方法
内存使用可以使用Runtime getRuntime() getTotalMemory() Runtime getRuntime() getFreeMemory()
纯JAVA是不能同操作系统打交到得 比如你要看CPU使用率 一般就是使用JNI技术(一些简单得系统熟悉 可以通过System getProperties()获得)
lishixinzhi/Article/program/Java/hx/201311/27126
java代码硬盘使用比例的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java代码硬盘使用比例是多少、java代码硬盘使用比例的信息别忘了在本站进行查找喔。







