Java Utililty Methods File to String

List of utility methods to do File to String

Description

The list of methods to do File to String are organized into topic(s).

Method

StringfileToString(File file, int max)
file To String
if (!file.exists() && file.canRead()) {
    throw new IllegalStateException("File " + file.getName() + " either does not exist or can not be read");
RandomAccessFile raf = new RandomAccessFile(file, "r");
if (raf.length() > max) {
    raf.close();
    throw new IllegalStateException("File " + file.getName() + " is to large (>" + max);
byte[] ba = new byte[(int) raf.length()];
raf.read(ba);
raf.close();
return new String(ba);
StringfileToString(File file, String charsetName)
file To String
FileInputStream in = new FileInputStream(file);
try {
    return allToString(new InputStreamReader(in, charsetName));
} finally {
    in.close();
StringfileToString(File file, String charsetName)
Transforms a file into a string.
FileInputStream fileInputStream = null;
try {
    try {
        fileInputStream = new FileInputStream(file);
        return streamToString(fileInputStream, charsetName);
    } finally {
        if (fileInputStream != null) {
            fileInputStream.close();
...
String[]fileToString(File files[])
file To String
String result[] = new String[files.length];
for (int i = 0; i < files.length; i++) {
    File file = files[i];
    result[i] = file.getAbsolutePath();
return result;
StringfileToString(final File file)
file To String
FileInputStream in = null;
int size = 0;
if (file.exists()) {
    in = new FileInputStream(file);
    size = (int) file.length();
try {
    int fRead = 0;
...
StringfileToString(final File file, final String charsetName)
Reads a file and returns it as a string.
return streamToString(fileToStream(file), charsetName);
StringfileToString(final String filePath)
Converts a file content to a string.
byte[] buffer = new byte[(int) new File(filePath).length()];
BufferedInputStream f = null;
try {
    f = new BufferedInputStream(new FileInputStream(filePath));
    f.read(buffer);
} finally {
    if (f != null) {
        f.close();
...
StringfileToString(InputStream file)
Pasa todo el contenido de un fichero a una cadena
String style = "";
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
    style += sc.nextLine() + "\n";
sc.close();
return style;
StringfileToString(InputStream inputStream)
file To String
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
return bufferedReader.lines().collect(Collectors.joining(System.lineSeparator()));
StringfileToString(String file)
Read a file into a String
StringBuilder lines = new StringBuilder();
FileReader fileReader = null;
BufferedReader bufferedReader = null;
String line = null;
try {
    fileReader = new FileReader(file);
    bufferedReader = new BufferedReader(fileReader);
    while ((line = bufferedReader.readLine()) != null) {
...