title: Read all linux partitions with Java
body: <p>I found this Java code which is used to get all partitions in Linux.</p>
  
  <pre><code>public class Partitions
  {
      // output
      // [8, 0, 312571224, sda, 8, 1, 716800, sda1, 8, 2, 5120000, sda2, 8, 3, 306733056, sda3]
      public static final String partitionsPath = "/proc/partitions";
      public static final String EMPTY = "";
      public static final String SPACE = " ";
      public static final String LINE_SEPARATOR = "line.separator";
  
      public ArrayList&lt;String&gt; gatherPartitionUsage()
      {
          ArrayList&lt;String&gt; data = new ArrayList&lt;&gt;();
          String[] tempData = null;
          String[] tempFile = null;
  
          tempFile = getContents(partitionsPath).split(System.getProperty(LINE_SEPARATOR));
  
          //parse the disk partitions
          for (int i = 2; i &lt; tempFile.length; i++)
          {
              tempData = tempFile[i].split(SPACE);
              data.addAll(Arrays.asList(tempData));
              data.removeAll(Collections.singleton(EMPTY));
          }
          return data;
      }
  
      static private synchronized String getContents(String path)
      {
          //...checks on aFile are elided
          StringBuilder contents = new StringBuilder();
  
          try
          {
              //use buffering, reading one line at a time
              //FileReader always assumes default encoding is OK!
              BufferedReader input = new BufferedReader(new FileReader(new File(path)));
              try
              {
                  String line = null; //not declared within while loop
                  /*
                   * readLine is a bit quirky : it returns the content of a line
                   * MINUS the newline. it returns null only for the END of the
                   * stream. it returns an empty String if two newlines appear in
                   * a row.
                   */
                  while ((line = input.readLine()) != null)
                  {
                      contents.append(line);
                      contents.append(System.getProperty(LINE_SEPARATOR));
                  }
              }
              finally
              {
                  input.close();
              }
          }
          catch (IOException ex)
          {
          }
  
          return contents.toString();
      }
  
      /*
       * Create a list with the partitions name to be used to find their
       * statistics in /proc/diskstats file
       */
      private ArrayList&lt;String&gt; getPartitionNames(ArrayList&lt;String&gt; data)
      {
          ArrayList&lt;String&gt; partitionsName = new ArrayList&lt;&gt;();
          for (String string : data)
          {
              if (!tryParseInt(string))
              {
                  partitionsName.add(string);
              }
          }
          return partitionsName;
      }
  
      /**
       * Try to parse a string to a integer.
       *
       * @param value
       * @return True if the string can be converted, false otherwise.
       */
      public boolean tryParseInt(String value)
      {
          try
          {
              Integer.parseInt(value);
              return true;
          }
          catch (NumberFormatException ex)
          {
              //  Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
              return false;
          }
      }
  }
  </code></pre>
  
  <p>This should be the output</p>
  
  <p>[8, 0, 312571224, sda, 8, 1, 716800, sda1, 8, 2, 5120000, sda2, 8, 3, 306733056, sda3]</p>
  
  <p>Can tell me this correct? Looks like it's working for now. Also how I can keep the returned result in a Java Map?</p>