Java Path File Content getFileContents(final String absFilePath)

Here you can find the source of getFileContents(final String absFilePath)

Description

get File Contents

License

Open Source License

Declaration

public static String getFileContents(final String absFilePath) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;

public class Main {
    public static String getFileContents(final String absFilePath) {

        String data = null;/*from w ww  .  j av a  2s.  c  om*/

        if (absFilePath != null) {

            File file = new File(absFilePath);

            if (file.isFile()) {

                byte[] buffer = new byte[4096];
                BufferedInputStream bis = null;

                try {

                    int readSize = 0;
                    StringBuilder builder = new StringBuilder();

                    bis = new BufferedInputStream(new FileInputStream(file), 4096);

                    while ((readSize = bis.read(buffer)) > 0) {
                        builder.append(new String(buffer, 0, readSize, Charset.forName("UTF-8")));
                    }

                    data = builder.toString();
                    bis.close();

                } catch (FileNotFoundException fnfe) {
                    fnfe.printStackTrace();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }

            } else {
                log("Specified path is not a file : " + absFilePath);
            }
        }

        return data;

    }

    public static void log(String str) {
        System.out.println(str);
    }
}

Related

  1. assertSameContent(String expectedFilePath, String actualFilePath)
  2. contentEquals(Path leftPath, Path rightPath)
  3. fileContent(final Path p)
  4. fileContent(final String filePath)
  5. getContent(Path path)
  6. getFileContents(Path file)
  7. getFileContents(String path)
  8. listUris(Path content)
  9. readContent(Path file)