The following is the most direct way of converting a File to a String in Java. The String will have the platform default encoding. The code is HERE
import java.io.*;
public class F2S {
/**
* Description of the Method
*
* @param file
* The file to be turned into a String
* @return The file as String encoded in the platform
* default encoding
*/
public static String fileToString(String file) {
String result = null;
DataInputStream in = null;
try {
File f = new File(file);
byte[] buffer = new byte[(int) f.length()];
in = new DataInputStream(new FileInputStream(f));
in.readFully(buffer);
result = new String(buffer);
} catch (IOException e) {
throw new RuntimeException("IO problem in fileToString", e);
} finally {
try {
in.close();
} catch (IOException e) { /* ignore it */
}
}
return result;
}
}
public class F2S {
/**
* Description of the Method
*
* @param file
* The file to be turned into a String
* @return The file as String encoded in the platform
* default encoding
*/
public static String fileToString(String file) {
String result = null;
DataInputStream in = null;
try {
File f = new File(file);
byte[] buffer = new byte[(int) f.length()];
in = new DataInputStream(new FileInputStream(f));
in.readFully(buffer);
result = new String(buffer);
} catch (IOException e) {
throw new RuntimeException("IO problem in fileToString", e);
} finally {
try {
in.close();
} catch (IOException e) { /* ignore it */
}
}
return result;
}
}