Java Path File Content getFileContents(String path)

Here you can find the source of getFileContents(String path)

Description

get File Contents

License

Apache License

Declaration

public static String getFileContents(String path) throws IOException 

Method Source Code


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

import java.io.BufferedReader;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.Files;

public class Main {
    public static String getFileContents(String path) throws IOException {
        String returnString = null;
        File file = new File(path);
        if (file.exists() && file.isFile()) {
            BufferedReader fileBufferedReader = null;
            try {
                fileBufferedReader = Files.newBufferedReader((FileSystems.getDefault().getPath(path)),
                        Charset.forName("utf-8"));

                StringBuffer sb = new StringBuffer();
                String line = null;
                while ((line = fileBufferedReader.readLine()) != null) {
                    sb.append(line).append("\n");
                }//w  w w. j a v a2s .  co m

                if (sb.length() > 0) {
                    returnString = sb.toString();
                }
            } finally {
                if (fileBufferedReader != null) {
                    fileBufferedReader.close();
                }
            }
        }

        return returnString;
    }
}

Related

  1. fileContent(final Path p)
  2. fileContent(final String filePath)
  3. getContent(Path path)
  4. getFileContents(final String absFilePath)
  5. getFileContents(Path file)
  6. listUris(Path content)
  7. readContent(Path file)
  8. saveByteArrayToFile(byte[] content, Path targetPath)
  9. saveFile(String workspacePath, byte[] content)