load file as string java

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

showing results for - "load file as string java"
Loïc
28 Jan 2018
1public String readFile(String filePath) {
2    String result = "";
3    try {
4        result = Files.readString(Paths.get(filePath));
5    } catch (IOException e) {
6        e.printStackTrace();
7    }
8    return result;
9}
Juliana
11 Aug 2020
1public static String loadFileAsString(String path) {
2  InputStream stream = YourClassName.class.getResourceAsStream(path);
3  try {
4    String result = new String(stream.readAllBytes());
5    return result;
6  } catch (IOException e) {
7    e.printStackTrace();
8    return null;
9  }
10}
11
Niclas
13 Mar 2017
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
queries leading to this page
java files read txt to stringhow to read full file into string javatext file to string javaconvert a file to string in javajava read file into a stringjava read file content as stringread a file and convert to string in javajava read whole file into stringtxt file read in java to stringhow to read file as string in javaconverting a file to a string javajava read file to string java 11java read a file as stringconvert file to string javaread file in string javaread file data as string in javajava read entire file as stringjava read file into stringhow to use text from a file to string in java java load file as stringread file from string javajava how to read a file into a stringload string from file javajava read text file to stringread file as string javajava read entire file into stringfile to a string javajava read file as stringread from file to string javahow to read a file into a string javajava read file to stringjava files to stringjava read from file to stringjava load string from filefile into string javaread file as string in java 8file to string jvaget file as string javaread file into string javajava read txt file into stringjava full read file to stringjava read file to string java 8java convert text file to stringread text file into string javajava 5 read file to stringjava read file contents to stringjava write read string from fileread a file as a string javafile to string and string to file javajava read a file as a stringfile to string java 8java read file to string with encodinghow to read in file as a string javaread file to string javajava 8 read file to stringfile to string javajava read file in stringjava file to stringread full file to string javahow to read a string in a file as string javahow to convert a file to string in javaload file as string javaread from text file java to stringhow to read a file in as a string javaread txt file as strign javahow to save read file to string in javaload file as string java