Java BufferedReader Read All readAllText(File file)

Here you can find the source of readAllText(File file)

Description

Read the given file and return its contents as string

License

Open Source License

Parameter

Parameter Description
file the file to read

Return

its contents as string

Declaration

public static String readAllText(File file) 

Method Source Code

//package com.java2s;
// under the terms of the GNU Lesser General Public License as published by

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.util.Vector;

public class Main {
    /**/*from  w w  w . j  a v  a  2 s.c  om*/
     * Read the given file and return its contents as string
     * 
     * @param file
     *            the file to read
     * @return its contents as string
     */
    public static String readAllText(File file) {
        if (file == null || !file.exists())
            return null;

        BufferedReader reader;
        StringBuilder builder = new StringBuilder();
        try {
            reader = new BufferedReader(new FileReader(file));
            String line;
            while ((line = reader.readLine()) != null)
                builder.append(line + "\n");
            reader.close();
        } catch (Exception e) {
        }
        return builder.toString();

    }

    /**
     * Returns the items of the given vector as comma separated list
     * 
     * @param items
     *            the vector to flatten
     * @return string of comma separated items
     */
    public static String toString(Vector<String> items) {
        if (items == null || items.size() == 0)
            return "";
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < items.size(); i++) {
            builder.append(items.get(i));
            if (i + 1 < items.size())
                builder.append(", ");
        }
        return builder.toString();
    }
}

Related

  1. readAllLines(InputStream is)
  2. readAllLines(String filepath)
  3. readAllStreamFromClasspathBaseResource(Class resourceBase, String dataLocation)
  4. readAllStreamsFromClasspathBaseResource(Class resourceBase, String[] dataLocations)
  5. readAllText(File file)
  6. readAllText(final InputStream inputStream)
  7. readAllText(InputStream stream)
  8. readAllText(String filePath)
  9. readAllText(String path)