扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
import java.io.File;
创新互联是一家专业提供东丽企业网站建设,专注与成都做网站、成都网站设计、成都h5网站建设、小程序制作等业务。10年已为东丽众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。
import java.text.NumberFormat;
public class Test {
public static void main(String[] args){
File f = new File("F://1.txt");
NumberFormat nt = NumberFormat.getPercentInstance();
// 精度
nt.setMinimumFractionDigits(2);
System.out.println(nt.format(memoryPercentage(f)));
}
public static double memoryPercentage(File f){
return ((double)f.length())/f.getTotalSpace();
}
}
1、先来先服务算法(FCFS)First Come First Service
这是一种比较简单的磁盘调度算法。它根据进程请求访问磁盘的先后次序进行调度。此算法的优点是公平、简单,且每个进程的请求都能依次得到处理,不会出现某一进程的请求长期得不到满足的情况。此算法由于未对寻道进行优化,在对磁盘的访问请求比较多的情况下,此算法将降低设备服务的吞吐量,致使平均寻道时间可能较长,但各进程得到服务的响应时间的变化幅度较小。
先来先服务 (125)86.147.91.177.94.150.102.175.130
[java] view plain copy print?
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盘 是可以的, 你有什么疑问 可追问我
import java.io.File;
import java.text.DecimalFormat;
/**
* User:
* Date: 2016/12/07
* Time: 14:29
*/
public class MainTest {
public static void main(String[] args) {
File[] roots = File.listRoots();
double constm = 1024 * 1024 * 1024 ;
double total = 0d;
for (File file : roots) {
System.out.println(file.getPath());
System.out.println("剩余空间 = " + doubleFormat(file.getFreeSpace()/constm)+" G");
System.out.println("已使用空间 = " + doubleFormat(file.getUsableSpace()/constm)+" G");
System.out.println(file.getPath()+"盘总大小 = " + doubleFormat(file.getTotalSpace()/constm)+" G");
System.out.println();
total += file.getTotalSpace();
}
System.out.println("你的硬盘总大小 = "+doubleFormat(total/constm));
}
private static String doubleFormat(double d){
DecimalFormat df = new DecimalFormat("0.##");
return df.format(d);
}
}
试一下。。。
java中可以使用file对象,获取当前电脑硬盘基本信息,示例如下:
import java.io.File;
public class DiskSpaceDetail {
public static void main(String[] args) {
File diskPartition = new File("C:");
long totalCapacity = diskPartition.getTotalSpace();
long freePartitionSpace = diskPartition.getFreeSpace();
long usablePatitionSpace = diskPartition.getUsableSpace();
System.out.println("**** Sizes in Mega Bytes ****\n");
System.out.println("Total C partition size : " + totalCapacity / (1024*1024) + " MB");
System.out.println("Usable Space : " + usablePatitionSpace / (1024 *1024) + " MB");
System.out.println("Free Space : " + freePartitionSpace / (1024 *1024) + " MB");
System.out.println("\n**** Sizes in Giga Bytes ****\n");
System.out.println("Total C partition size : " + totalCapacity / (1024*1024*1024) + " GB");
System.out.println("Usable Space : " + usablePatitionSpace / (1024 *1024*1024) + " GB");
System.out.println("Free Space : " + freePartitionSpace / (1024 *1024*1024) + " GB");
}
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;
public class Test {
public static void main(String[] args) {
HardDiskAction action = new HardDiskAction();
HardDisk hardDisk;
for (int i = 0; i 5; i++) {
hardDisk = new HardDisk();
hardDisk.setName("name" + i);
hardDisk.setPrice(100 + i);
hardDisk.setCapacity(200 + i);
hardDisk.setDetail("this is name" + i + " detail");
action.add(hardDisk);
}
for (int i = 5; i 10; i++) {
hardDisk = new HardDisk();
hardDisk.setName("name" + i);
hardDisk.setPrice(100 - i);
hardDisk.setCapacity(200 - i);
hardDisk.setDetail("this is name" + i + " detail");
action.add(hardDisk);
}
action.printAllDiskDetail();
}
}
class HardDiskAction {
private MapString, HardDisk hardDisks = new TreeMapString, HardDisk();
public void add(HardDisk HardDisk) {
hardDisks.put(HardDisk.getName(), HardDisk);
}
public void remove(String name) {
hardDisks.remove(name);
}
public void printAllDiskDetail() {
ListHardDisk list = new ArrayListHardDisk();
Iterator it = hardDisks.entrySet().iterator();
while (it.hasNext()) {
list.add((HardDisk) ((Entry) it.next()).getValue());
}
int length = list.size();
HardDisk hardDisk;
for (int i = 0; i length; i++) {
for (int j = i + 1; j length; j++) {
double previousPrice = list.get(i).getPrice();
double nextPrice = list.get(j).getPrice();
int previousSize = list.get(i).getCapacity();
int nextSize = list.get(j).getCapacity();
if (previousPrice nextPrice
|| (previousPrice == nextPrice previousSize nextSize)) {// 判断价格和大小来排序
hardDisk = list.get(i);
list.set(i, list.get(j));
list.set(j, hardDisk);
}
}
}
for (HardDisk disk : list) {
System.out.println("name:" + disk.getName() + ",price:"
+ disk.getPrice() + ",Capacity:" + disk.getCapacity()
+ "G,detail:" + disk.getDetail());
}
}
}
class HardDisk {
private String name;
private double price;
private int capacity;
private String detail;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流