Java File to String fileToString(String path)

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

Description

Reads file pointed by path and returns the content of the file in String format.

License

Apache License

Parameter

Parameter Description
path Path to file with UTF-8 encoded JSON object.

Return

String with value representing content of file pointed by path.

Declaration

private static String fileToString(String path) 

Method Source Code


//package com.java2s;
/*/*from w  w  w .j  a  v a  2  s .co  m*/
 * Copyright 2016 Poznan Supercomputing and Networking Center (PSNC)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 */

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

public class Main {
    /**
     * Reads file pointed by path and returns the content of the file in String format.
     * (The assumption is that the file contains UTF-8 encoded JSON object.)
     * 
     * @param path Path to file with UTF-8 encoded JSON object.
     * 
     * @return String with value representing content of file pointed by path.
     */
    private static String fileToString(String path) {

        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(path);
        } catch (FileNotFoundException ex) {
            throw new RuntimeException(String.format("Failed to open %s file.", path), ex);
        }

        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        } catch (UnsupportedEncodingException ex1) {
            try {
                inputStream.close();
            } catch (IOException ex2) {
                // empty, on purpose
            }
            throw new RuntimeException(String.format(
                    "File with definition of all available profiles %s is encoded in unsupported format",
                    inputStream), ex1);
        }

        /*
         * each line read will be added to that buffer
         */
        StringBuffer stringBuilder = new StringBuffer();

        /*
         * iterate through all available lines
         */
        try {

            String line = bufferedReader.readLine();

            while (line != null) {
                stringBuilder.append(line);
                line = bufferedReader.readLine();
            }

        } catch (IOException ex) {

            throw new RuntimeException(
                    String.format("Failed to read definition of all available profiles from %s", path), ex);

        } finally {

            try {
                inputStream.close();
            } catch (IOException ex1) {
                // empty, on purpose
            }

        }

        return stringBuilder.toString();

    }
}

Related

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