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(String path)
Reads file pointed by path and returns the content of the file in String format.
InputStream inputStream = null;
try {
    inputStream = new FileInputStream(path);
} catch (FileNotFoundException ex) {
    throw new RuntimeException(String.format("Failed to open %s file.", path), ex);
BufferedReader bufferedReader = null;
try {
...
StringfileToString(String pathname)
file To String
File file = new File(pathname);
StringBuilder fileContents = new StringBuilder((int) file.length());
Scanner scanner = new Scanner(file);
String lineSeparator = System.getProperty("line.separator");
try {
    while (scanner.hasNextLine()) {
        fileContents.append(scanner.nextLine() + lineSeparator);
    return fileContents.toString();
} finally {
    scanner.close();
StringfileToString(String strPath)
Generates a String representing the file in a path
StringBuffer strMyFile = new StringBuffer("");
try {
    File f = new File(strPath);
    FileInputStream fis = new FileInputStream(f);
    InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
    final BufferedReader mybr = new BufferedReader(isr);
    String strTemp = mybr.readLine();
    strMyFile.append(strTemp);
...
String[]fileToStringArray(String fileName)
file To String Array
String data = fileToString(fileName);
String str = cleanText(data);
String[] wordArray = str.split(" ");
return wordArray;
StringBufferfileToStringBuffer(File file)
file To String Buffer
BufferedReader br = new BufferedReader(new FileReader(file));
StringBuffer sb = new StringBuffer();
try {
    String s = null;
    while ((s = br.readLine()) != null) {
        sb.append(s + "\n");
} finally {
...
ListfileToStringList(File f)
Reads an UTF-8 encoded text file and returns its lines as list of String objects.
List<String> lines = new ArrayList<String>();
if (null != f && f.exists() && f.isFile()) {
    Scanner s = null;
    try {
        s = new Scanner(f, "UTF-8");
        while (s.hasNextLine()) {
            lines.add(s.nextLine());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (null != s) {
            s.close();
return lines;
ListfileToStringList(String filePath)
Reads the contents of the specified file
LineNumberReader reader = new LineNumberReader(new FileReader(filePath));
List<String> lines = new ArrayList<String>();
String line;
while ((line = reader.readLine()) != null)
    lines.add(line);
reader.close();
return lines;
StringfileToStringOneLine(String path)
file To String One Line
String string = new String();
try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
    String line;
    while ((line = reader.readLine()) != null) {
        string += line;
    reader.close();
...
StringreadFileAsString(String filePath)
Read a file content to a string variable
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = new BufferedInputStream(new FileInputStream(filePath));
byte[] buf = new byte[BUFFER_SIZE];
int numOfBytes = is.read(buf);
while (numOfBytes != -1) {
    baos.write(buf, 0, numOfBytes);
    numOfBytes = is.read(buf);
is.close();
return new String(baos.toByteArray(), "UTF-8");
StringreadFileAsString(String filePath)
Reads the specified file into a string
File file = new File(filePath);
if (!file.exists()) {
    return "";
byte[] buffer = new byte[(int) new File(filePath).length()];
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(filePath));
bufferedInputStream.read(buffer);
bufferedInputStream.close();
...