Java BufferedReader Read All readAllTextFromSystemResource(String path)

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

Description

read All Text From System Resource

License

Open Source License

Declaration

public static String readAllTextFromSystemResource(String path) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source 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 readAllTextFromSystemResource(String path) throws IOException {
        InputStream stream = ClassLoader.getSystemResourceAsStream(path);
        try {//www . j  av  a2 s .co  m
            return readAllText(stream);
        } finally {
            stream.close();
        }
    }

    public static String readAllText(File file) throws IOException {
        InputStream stream = new FileInputStream(file);
        try {
            return readAllText(stream);
        } finally {
            stream.close();
        }
    }

    /**
     * Reads all text from the stream using the default charset. Does not close
     * the stream.
     */
    public static String readAllText(InputStream stream) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
        StringBuilder result = new StringBuilder();
        while (true) {
            String s = reader.readLine();
            if (s == null) {
                return result.toString();
            }
            result.append(s);
            result.append('\n');
        }
    }
}

Related

  1. readAllText(File file)
  2. readAllText(final InputStream inputStream)
  3. readAllText(InputStream stream)
  4. readAllText(String filePath)
  5. readAllText(String path)