Java: Inserire il contenuto di un file in un oggetto String
6 ottobre 2010
Spesso mi è capitato di dover creare una stringa con il contenuto di un file, magari anche un documento XML. Il metodo che uso è il seguente:
protected static String fileToString(String filePath) {
BufferedReader reader = null;
StringBuffer fileData = null;
try {
fileData = new StringBuffer(1000);
reader = new BufferedReader(new FileReader(filePath));
char[] buf = new char[1024];
int numRead = 0;
while ((numRead = reader.read(buf)) != -1) {
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
buf = new char[1024];
}
return fileData.toString();
} catch (IOException ex) {
System.out.println("Eccezione in readFileAsString: " + ex.toString());
return null;
}
}
Il parametro prevede il passaggio del nome del file comprensivo del suo percorso.
Le classi da importare sono:
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException;


