代碼1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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" ); } } |
運行結果
代碼2:
1
2
3
4
5
6
7
8
9
10
11
|
public class freediskspace { public static void main(string[] args) { file file = new file( "c:" ); long totalspace = file.gettotalspace(); long freespace = file.getfreespace(); long usedspace = totalspace - freespace; system.out.println( "總空間大小 : " + totalspace / 1024 / 1024 / 1024 + "g" ); system.out.println( "剩余空間大小 : " + freespace / 1024 / 1024 / 1024 + "g" ); system.out.println( "已用空間大小 : " + usedspace / 1024 / 1024 / 1024 + "g" ); } } |
結果:
總結
哈哈,讓大家見笑了。
以上就是本文關于java獲取磁盤空間的兩種代碼示例的全部內容,希望對大家有所幫助。如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://www.open-open.com/code/view/1420352276109