Junior — Middle
What method can be used to load the contents of a text file into a string variable in your programming language?
sobes.tech AI
Answer from AI
In Java, to load the contents of a text file into a string, you can use the Files class from the java.nio.file package:
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.IOException;
public class ReadFileExample {
public static void main(String[] args) throws IOException {
String content = Files.readString(Paths.get("path/to/file.txt"));
System.out.println(content);
}
}
This method reads the entire file into a string, which is convenient for small files.