{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "f6d4bb136fef4aae992e14e46832cdcb",
  "keywords": "计算机",
  "title": "获取硬盘信息",
  "desc": [
    {
      "content": "\n获取硬盘的分区信息，以及每个分区的磁盘空间及剩余空间",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "import java.io.File;\nimport java.text.DecimalFormat;\npublic class Ypxx {\n\tpublic static void main(String[] args) {\n\t\tFile[] roots = File.listRoots();\n\t\tfor (File file : roots) {\n\t\t\tSystem.out.println(file.getPath() + \"信息如下:\");\n\t\t\tlong free = file.getFreeSpace();\n\t\t\tlong total = file.getTotalSpace();\n\t\t\tlong use = total - free;\n\t\t\tSystem.out.println(\"空闲未使用 = \" + change(free) + \"G\");\n\t\t\tSystem.out.println(\"已经使用 = \" + change(use) + \"G\");\n\t\t\tSystem.out.println(\"总容量 = \" + change(total) + \"G\");\n\t\t\tSystem.out.println(\"使用百分比 = \" + bfb(use, total));\n\t\t\tSystem.out.println();\n\t\t}\n\t}\n\tpublic static long change(long num) {\n\t\treturn num / 1024 / 1024 / 1024;\n\t}\n\tpublic static String bfb(Object num1, Object num2) {\n\t\tdouble val1 = Double.valueOf(num1.toString());\n\t\tdouble val2 = Double.valueOf(num2.toString());\n\t\tif (val2 == 0) {\n\t\t\treturn \"0.0%\";\n\t\t} else {\n\t\t\tDecimalFormat df = new DecimalFormat(\"#0.00\");\n\t\t\treturn df.format(val1 / val2 * 100) + \"%\";\n\t\t}\n\t}\n}",
    "language": "java"
  },
  "node_id": "dailycode-4373e77e72654321846d4cb7d92b7935",
  "license": "csdn.net",
  "created_at": 1637894160,
  "topic_link": "https://bbs.csdn.net/topics/600470294"
}