Close

Java IO & NIO - Files.readAllBytes() Examples

Java IO & NIO Java Java API 


Class:

java.nio.file.Files

java.lang.Objectjava.lang.Objectjava.nio.file.Filesjava.nio.file.FilesLogicBig

Method:

public static byte[] readAllBytes(Path path)
                           throws IOException

Reads all the bytes from a file.

Parameters:
path - the path to the file
Returns:
a byte array containing the bytes read from the file


Examples


package com.logicbig.example.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class ReadAllBytesExample {

public static void main(String... args) throws IOException {
Path path = Files.createTempFile("test-file", ".txt");
//write
Files.write(path, "Some test data..".getBytes());
//read
byte[] bytes = Files.readAllBytes(path);
System.out.println(new String(bytes));
}
}

Output

Some test data..




See Also