Java File to String readFileAsString(String path)

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

Description

read File As String

License

Apache License

Declaration

public static String readFileAsString(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.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
    public static String readFileAsString(String path) throws IOException {
        StringBuffer sb = new StringBuffer();
        InputStream in = new FileInputStream(path);
        BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
        String line = "";
        while ((line = reader.readLine()) != null) {
            line = line.trim();//from  w  ww.j  av a  2s  .  c o  m
            if (line == null || "".equals(line)) {
                continue;
            }
            sb.append(line);
        }
        reader.close();
        return sb.toString();
    }

    public static String readFileAsString(File file) throws IOException {
        StringBuffer sb = new StringBuffer();
        InputStream in = new FileInputStream(file);
        BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
        String line = "";
        while ((line = reader.readLine()) != null) {
            line = line.trim();
            if (line == null || "".equals(line)) {
                continue;
            }
            sb.append(line);
        }
        reader.close();
        return sb.toString();
    }
}

Related

  1. readFileAsString(String fname)
  2. readFileAsString(String fpath)
  3. readFileAsString(String path)
  4. readFileAsString(String path)
  5. readFileAsString(String path)