Answers for "load file as string java"

1

java read file to string

public String readFile(String filePath) {
    String result = "";
    try {
        result = Files.readString(Paths.get(filePath));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}
Posted by: Guest on June-06-2021
1

load file as string java

public static String loadFileAsString(String path) {
  InputStream stream = YourClassName.class.getResourceAsStream(path);
  try {
    String result = new String(stream.readAllBytes());
    return result;
  } catch (IOException e) {
    e.printStackTrace();
    return null;
  }
}
Posted by: Guest on July-06-2021
0

java load entire file to string

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
 
public class ReadFileToString 
{
    public static void main(String[] args) 
    {
        String filePath = "c:/temp/data.txt";
 
        System.out.println( readAllBytesJava7( filePath ) );
    }
 
    //Read file content into string with - Files.readAllBytes(Path path)
 
    private static String readAllBytesJava7(String filePath) 
    {
        String content = "";
 
        try
        {
            content = new String ( Files.readAllBytes( Paths.get(filePath) ) );
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
 
        return content;
    }
}
Posted by: Guest on May-05-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language