title: getting outOfMemoryError: Java Heap Space while reading file in java
body: <p>I have written a program to get data from the file. Whenever I am trying to find data from small files my program works properly but error occurs when file is large. I tried to increase heap size in eclipse but nothing waorks.Below is my code.</p>
  
  <pre><code> public static String getHiddenDataFromImage(String imagePath)
        {
          String dataContents = null;
          try
          {
            File file = new File(imagePath);
            byte[] fileData = new byte[(int)file.length()];
            InputStream inStream = new FileInputStream(file);
            inStream.read(fileData);
            inStream.close();
            String tempFileData = new String(fileData);
            String finalData = tempFileData.substring(tempFileData.indexOf(extraStr) + extraStr.length(), tempFileData.length());
            byte[] bytefinalData=finalData.getBytes();
            byte[] temp = (byte[]) new Base64().decode(bytefinalData);
            dataContents = new String(temp);
          }
          catch (Exception e)
          {
            e.printStackTrace();
          }
  
          return dataContents;
        }
  
  
       public static void main(String[] args){
      System.out.println(ImageAnalyzerUtil.getHiddenDataFromImage2("C:/example.m4b"));
    }
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre><code>Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
      at ImageAnalyzerUtil.getHiddenDataFromImage(ImageAnalyzerUtil.java:199)
      at ImageAnalyzerUtil.main(ImageAnalyzerUtil.java:332)
  </code></pre>
  
  <p>line 199 is  byte[] fileData = new byte[(int)file.length()];</p>