Tag Archive: read file


Read file completely in Java 8

In Java 8, there’s a short hand method to read a file in one step from disk:

byte[] b = Files.readAllBytes(Paths.get("test.txt"));

Read file completely in Java

FileInputStream fis = new FileInputStream("file.xml");
byte[] b = new byte[4096];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (fis.available() > 0) {
  int n = fis.read(b);
  if (n == -1)
  break;
  baos.write(b, 0, n);
}
System.out.println(baos.toString());