You can read the file and calculate the value of the hash as you go.
byte[] buffer= new byte[8192];
int count;
MessageDigest digest = MessageDigest.getInstance("SHA-256");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName));
while ((count = bis.read(buffer)) > 0) {
digest.update(buffer, 0, count);
}
bis.close();
byte[] hash = digest.digest();
System.out.println(new BASE64Encoder().encode(hash));
This doesn't assume anything about character sets or about the file fitting into memory, and it doesn't ignore line terminators either.
Or you can use a DigestInputStream.