Java Text File Read fileAsString(String filePath)

Here you can find the source of fileAsString(String filePath)

Description

Read a file, putting its contents into a String.

License

Open Source License

Parameter

Parameter Description
filePath A string containing a platform-acceptable file path.

Return

The file contents, converted to a String, using the default locale. Returns null if there are any problems.

Declaration

public static String fileAsString(String filePath) 

Method Source Code


//package com.java2s;
import java.io.File;
import java.io.FileInputStream;

public class Main {
    /**/*from  w  w w .j  a  v a  2s.c  o  m*/
    Read a file, putting its contents into a String.
        
    @param filePath A string containing a platform-acceptable
    file path.
        
    @return The file contents, converted to a String, using
    the default locale.  Returns null if there are any
    problems.
    */
    public static String fileAsString(String filePath) {
        String retVal = null;

        if (filePath == null) {
            return null;
        }

        try {
            FileInputStream fin = new FileInputStream(filePath);
            return fileAsString(fin);
        } catch (Exception exc) {
            // just go ahead and return the null
        }

        return retVal;
    }

    /**
    Read a file, putting its contents into a String.
        
    @param dir Directory where filepath is rooted
    @param filepath The file we want to read
        
    @return The file contents, converted to a String, using
    the default locale.  Returns null if there are any
    problems.
    */
    public static String fileAsString(File dir, String filepath) {
        File theF = new File(dir, filepath);
        return fileAsString(theF);
    }

    /**
    Read a file, putting its contents into a String.
        
    @param infile The file we want to read
        
    @return The file contents, converted to a String, using
    the default locale.  Returns null if there are any
    problems.
    */
    public static String fileAsString(File infile) {
        String retVal = null;

        try {
            FileInputStream fin = new FileInputStream(infile);
            return fileAsString(fin);
        } catch (Exception exc) {
            // just go ahead and return the null
        }

        return retVal;
    }

    /**
    Read a file, putting its contents into a String.
        
    @param fin An input stream.  This method closes the stream
        
    @return The file contents, converted to a String, using
    the default locale.  Returns null if there are any
    problems.
    */
    public static String fileAsString(FileInputStream fin) {
        String retVal = null;

        try {
            int avail = fin.available();
            byte[] bb = new byte[avail];
            fin.read(bb);
            fin.close();

            retVal = new String(bb);
        } catch (Exception exc) {
            // just go ahead and return the null
        }

        return retVal;
    }
}

Related

  1. fileAsString(File file)
  2. fileAsString(String fileName)
  3. fileContentsToString(File errFile)
  4. fileContentsToString(String file)
  5. getChars(File file)
  6. getChars(Reader r)