Java Path File Read nio readFile(String path)

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

Description

Reads the contents of a file into a string.

License

Apache License

Parameter

Parameter Description
path The path of the file to read using default character encoding.

Exception

Parameter Description
IOException If a problem occurred trying to read from the reader.

Return

The contents of the reader as a string, or null if file does not exist.

Declaration

public static String readFile(String path) throws IOException 

Method Source Code


//package com.java2s;
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *

import java.io.*;
import java.nio.charset.*;

public class Main {
    /**//from w  w w  .  j  ava  2s.com
     * Reads the contents of a file into a string.
     *
     * @param path The path of the file to read using default character encoding.
     * @return The contents of the reader as a string, or <jk>null</jk> if file does not exist.
     * @throws IOException If a problem occurred trying to read from the reader.
     */
    public static String readFile(String path) throws IOException {
        return read(new File(path));
    }

    /**
     * Reads the contents of a file into a string.
     *
     * @param in The file to read using default character encoding.
     * @return The contents of the reader as a string, or <jk>null</jk> if file does not exist.
     * @throws IOException If a problem occurred trying to read from the reader.
     */
    public static String read(File in) throws IOException {
        if (in == null || !in.exists())
            return null;
        Reader r = new InputStreamReader(new FileInputStream(in), Charset.defaultCharset());
        return read(r, 0, 1024);
    }

    /**
     * Reads the contents of a reader into a string.
     *
     * @param in The input reader.
     * @return The contents of the reader as a string.
     * @throws IOException If a problem occurred trying to read from the reader.
     */
    public static String read(Reader in) throws IOException {
        return read(in, 0, 1024);
    }

    /**
     * Reads the contents of an input stream into a string using the specified charset.
     *
     * @param in The input stream.
     * @param cs The charset of the contents of the input stream.
     * @return The contents of the reader as a string.  <jk>null</jk> if input stream was null.
     * @throws IOException If a problem occurred trying to read from the input stream.
     */
    public static String read(InputStream in, Charset cs) throws IOException {
        if (in == null)
            return null;
        return read(new InputStreamReader(in, cs));
    }

    /**
     * Reads the contents of an input stream into a string using the system default charset.
     *
     * @param in The input stream.
     * @return The contents of the reader as a string, or <jk>null</jk> if the input stream is null.
     * @throws IOException If a problem occurred trying to read from the input stream.
     */
    public static String read(InputStream in) throws IOException {
        if (in == null)
            return null;
        return read(new InputStreamReader(in, Charset.defaultCharset()));
    }

    /**
     * Reads the specified input into a {@link String} until the end of the input is reached.
     * <p>
     * The {@code Reader} is automatically closed.
     * <p>
     * If the {@code Reader} is not an instance of a {@code BufferedReader}, then it gets wrapped in a {@code BufferedReader}.
     *
     * @param in The input reader.
     * @param length Specify a positive number if the length of the input is known.
     * @param bufferSize Specify the buffer size to use.
     * @return The contents of the reader as a string.  <jk>null</jk> if reader was null.
     * @throws IOException If a problem occurred trying to read from the reader.
     */
    public static String read(Reader in, int length, int bufferSize) throws IOException {
        if (in == null)
            return null;
        length = (length <= 0 ? bufferSize : length);
        StringBuilder sb = new StringBuilder(length); // Assume they're ASCII characters.
        try {
            char[] buf = new char[Math.min(bufferSize, length)];
            int i = 0;
            while ((i = in.read(buf)) != -1)
                sb.append(buf, 0, i);
            return sb.toString();
        } finally {
            in.close();
        }
    }
}

Related

  1. readers(String path)
  2. readFile(Path directory, String... parts)
  3. readFile(Path file)
  4. readFile(Path path)
  5. readFile(Path path)
  6. readFile(String path)
  7. readFile(String path)
  8. readFile(String path)
  9. readFile(String path)