Java File to String fileToString(String strPath)

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

Description

Generates a String representing the file in a path

License

Open Source License

Parameter

Parameter Description
strPath a parameter

Return

file to a String

Declaration

public static String fileToString(String strPath) throws FileNotFoundException 

Method Source Code


//package com.java2s;
/*/*  w ww  .j a  v a2  s  .  c  om*/
 *************************************************************************
 * The contents of this file are subject to the Openbravo  Public  License
 * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
 * Version 1.1  with a permitted attribution clause; you may not  use this
 * file except in compliance with the License. You  may  obtain  a copy of
 * the License at http://www.openbravo.com/legal/license.html 
 * Software distributed under the License  is  distributed  on  an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific  language  governing  rights  and  limitations
 * under the License. 
 * The Original Code is Openbravo ERP. 
 * The Initial Developer of the Original Code is Openbravo SLU 
 * All portions are Copyright (C) 2001-2012 Openbravo SLU
 * All Rights Reserved. 
 * Contributor(s):  ______________________________________.
 ************************************************************************
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    /**
     * Generates a String representing the file in a path
     * 
     * @param strPath
     * @return file to a String
     */
    public static String fileToString(String strPath) throws FileNotFoundException {
        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);
            while (strTemp != null) {
                strTemp = mybr.readLine();
                if (strTemp != null)
                    strMyFile.append("\n").append(strTemp);
                else {
                    mybr.close();
                    fis.close();
                }
            }
        } catch (final IOException e) {
            e.printStackTrace();
        }
        return strMyFile.toString();
    }
}

Related

  1. fileToString(String filePath)
  2. fileToString(String filePath, String encoding)
  3. fileToString(String nomeArquivo)
  4. fileToString(String path)
  5. fileToString(String pathname)
  6. fileToStringArray(String fileName)
  7. fileToStringBuffer(File file)
  8. fileToStringList(File f)
  9. fileToStringList(String filePath)