All Downloads are FREE. Search and download functionalities are using the official Maven repository.

in.ashwanthkumar.utils.io.IO Maven / Gradle / Ivy

There is a newer version: 0.1.0
Show newest version
package in.ashwanthkumar.utils.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class IO {
    /**
     * Read the entire file as a String
     *
     * @param source
     * @return
     * @throws IOException
     */
    public static String fromFile(String source) throws IOException {
        return fromFile(new File(source), false);
    }

    /**
     * Read the entire file as a string.
     *
     * @param source
     * @param hasNewLine Should we maintain the \n in the final string?
     * @return
     * @throws IOException
     */
    public static String fromFile(String source, boolean hasNewLine) throws IOException {
        return fromFile(new File(source), hasNewLine);
    }

    /**
     * Read the entire file as a string
     *
     * @param input
     * @param hasNewLine
     * @return
     * @throws IOException
     */
    public static String fromFile(File input, boolean hasNewLine) throws IOException {
        String EOL = "";
        if (hasNewLine) {
            EOL = "\n";
        }

        StringBuilder contents = new StringBuilder();
        BufferedReader reader = new BufferedReader(new FileReader(input));
        String line;
        while ((line = reader.readLine()) != null) {
            contents.append(line);
            contents.append(EOL);
        }
        return contents.toString();
    }

    /**
     * Read the file as a List<String> with each line as an item.
     *
     * @param source
     * @return
     * @throws IOException
     */
    public static List linesFromFile(String source) throws IOException {
        return linesFromFile(new File(source));
    }

    public static List linesFromFile(File source) throws IOException {
        ArrayList contents = new ArrayList();
        BufferedReader reader = new BufferedReader(new FileReader(source));
        String line;
        while ((line = reader.readLine()) != null) {
            contents.add(line);
        }

        return contents;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy