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 filename)

Convert the content of the given file to string.

BufferedReader reader = null;
try {
    reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filename))));
    StringBuffer sb = new StringBuffer();
    char[] buffer = new char[1024];
    int k = 0;
    while ((k = reader.read(buffer)) != -1) {
        sb.append(buffer, 0, k);
...
StringfileToString(String fileName)
file To String
String ret = null;
try {
    InputStream in = new FileInputStream(fileName);
    ret = streamToString(in);
    in.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
...
StringfileToString(String fileName)
file To String
StringBuffer result = new StringBuffer();
BufferedReader in = new BufferedReader(new FileReader(fileName));
char[] buffer = new char[1000];
int readedChars = 0;
try {
    while ((readedChars = in.read(buffer)) > 0) {
        result.append(buffer, 0, readedChars);
} finally {
    in.close();
return result.toString();
StringfileToString(String filename)
Read the contents of a file into a single Java String.
BufferedReader in = new BufferedReader(new FileReader(filename));
StringBuffer sb = new StringBuffer();
String str = null;
while ((str = in.readLine()) != null) {
    sb.append(str);
    sb.append('\n');
in.close();
...
StringfileToString(String fileName)
file To String
File file = new File(fileName);
return fileToString(file);
StringfileToString(String fileName)
Converts a file specified by the path, to the String.
if (fileName != null) {
    byte[] utf8Bytes;
    String sFile = new String();
    try {
        FileInputStream fis = new FileInputStream(fileName);
        int noOfBytes = fis.available();
        if (noOfBytes > 0) {
            utf8Bytes = new byte[noOfBytes];
...
StringfileToString(String fileName, String timestamp, String adapterName)
file To String
StringBuffer sb = new StringBuffer();
BufferedReader in = new BufferedReader(new FileReader(fileName));
String line;
boolean timestampActive = false;
boolean adapterNameActive = false;
while ((line = in.readLine()) != null) {
    line = line.trim();
    if (line.length() > 0) {
...
StringfileToString(String filePath)
file To String
File f = new File(filePath);
BufferedReader br = new BufferedReader(new FileReader(f));
try {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();
    while (line != null) {
        sb.append(line);
        sb.append("\n");
...
StringfileToString(String filePath, String encoding)
file To String
InputStreamReader reader = null;
StringWriter writer = new StringWriter();
try {
    if (encoding == null || "".equals(encoding)) {
        reader = new InputStreamReader(new FileInputStream(new File(filePath)), encoding);
    } else {
        reader = new InputStreamReader(new FileInputStream(new File(filePath)));
    char[] buffer = new char[1024];
    int n = 0;
    while (-1 != (n = reader.read(buffer))) {
        writer.write(buffer, 0, n);
} catch (Exception e) {
    e.printStackTrace();
    return null;
} finally {
    if (reader != null) {
        try {
            reader.close();
        } catch (Exception e2) {
            e2.printStackTrace();
if (writer != null) {
    return writer.toString();
} else {
    return null;
StringfileToString(String nomeArquivo)
Metodo que recebe caminho e nome de um arquivo XML e devolve um String com seu conteudo
InputStream input;
InputStreamReader reader;
BufferedReader buffer;
String texto = "", linha;
try {
    input = new FileInputStream(nomeArquivo);
    reader = new InputStreamReader(input);
    buffer = new BufferedReader(reader);
...