java load entire file to string

Solutions on MaxInterview for java load entire file to string by the best coders in the world

showing results for - "java load entire file to string"
Yannick
20 Nov 2018
1import java.io.IOException;
2import java.nio.file.Files;
3import java.nio.file.Paths;
4 
5public class ReadFileToString 
6{
7    public static void main(String[] args) 
8    {
9        String filePath = "c:/temp/data.txt";
10 
11        System.out.println( readAllBytesJava7( filePath ) );
12    }
13 
14    //Read file content into string with - Files.readAllBytes(Path path)
15 
16    private static String readAllBytesJava7(String filePath) 
17    {
18        String content = "";
19 
20        try
21        {
22            content = new String ( Files.readAllBytes( Paths.get(filePath) ) );
23        } 
24        catch (IOException e) 
25        {
26            e.printStackTrace();
27        }
28 
29        return content;
30    }
31}
32