Java File to String fileToString(String fileName)

Here you can find the source of fileToString(String fileName)

Description

Converts a file specified by the path, to the String.

License

Apache License

Declaration

public static String fileToString(String fileName) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.FileInputStream;

public class Main {
    /**/*from  w  w  w.  j av  a  2s. c  om*/
     * Converts a file specified by the path, to the String.
     */
    public static String fileToString(String fileName) {
        if (fileName != null) {
            // String sLine;
            byte[] utf8Bytes;
            String sFile = new String();
            // Reading input by lines:
            try {
                FileInputStream fis = new FileInputStream(fileName);
                int noOfBytes = fis.available();
                if (noOfBytes > 0) {
                    utf8Bytes = new byte[noOfBytes];
                    fis.read(utf8Bytes);
                    sFile = new String(utf8Bytes, "UTF8");
                }
            } catch (Exception ex) {
                return null;
            }
            return sFile;
        }
        return null;
    }
}

Related

  1. fileToString(String filename)
  2. fileToString(String fileName)
  3. fileToString(String fileName)
  4. fileToString(String filename)
  5. fileToString(String fileName)
  6. fileToString(String fileName, String timestamp, String adapterName)
  7. fileToString(String filePath)
  8. fileToString(String filePath, String encoding)
  9. fileToString(String nomeArquivo)