Java Text File Load loadText(File file)

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

Description

Loads text into a string from a file

License

Open Source License

Parameter

Parameter Description
file Description of the Parameter

Exception

Parameter Description

Return

Description of the Return Value

Declaration

public static String loadText(File file) throws java.io.IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.util.ArrayList;

public class Main {
    /**/*from www .  jav  a2 s.c  om*/
     * Loads text into a string from a file
     *
     * @param file Description of the Parameter
     * @return Description of the Return Value
     * @throws java.io.IOException Description of the Exception
     */
    public static String loadText(File file) throws java.io.IOException {
        String ls = System.getProperty("line.separator");
        StringBuffer text = new StringBuffer();
        BufferedReader in = new BufferedReader(new FileReader(file));
        String line = null;
        boolean hasLine = false;
        while ((line = in.readLine()) != null) {
            if (hasLine) {
                text.append(ls);
            }
            text.append(line);
            hasLine = true;
        }
        in.close();
        return text.toString();
    }

    /**
     * Loads text into a string from a file
     *
     * @param filename Description of the Parameter
     * @return Description of the Return Value
     * @throws java.io.IOException Description of the Exception
     */
    public static String loadText(String filename) throws java.io.IOException {
        String ls = System.getProperty("line.separator");
        StringBuffer text = new StringBuffer();
        BufferedReader in = new BufferedReader(new FileReader(filename));
        String line = null;
        boolean hasLine = false;
        while ((line = in.readLine()) != null) {
            if (hasLine) {
                text.append(ls);
            }
            text.append(line);
            hasLine = true;
        }
        in.close();
        return text.toString();
    }

    /**
     * Description of the Method
     *
     * @param filename       Description of the Parameter
     * @param lines          Description of the Parameter
     * @param ignoreComments Description of the Parameter
     * @throws java.io.IOException Description of the Exception
     */
    public static void loadText(String filename, ArrayList lines, boolean ignoreComments)
            throws java.io.IOException {
        BufferedReader in = new BufferedReader(new FileReader(filename));
        String line = null;
        while ((line = in.readLine()) != null) {
            if (!ignoreComments || (ignoreComments && !line.startsWith("#") && !"".equals(line.trim()))) {
                lines.add(line);
            }
        }
        in.close();
    }

    /**
     * Description of the Method
     *
     * @param s Description of Parameter
     * @return Description of the Returned Value
     */
    public static String toString(String s) {
        if (s != null) {
            return (s);
        } else {
            return ("");
        }
    }
}

Related

  1. loadText(File f)
  2. loadText(File file)
  3. loadText(File file)
  4. loadText(File file, int bufsize)
  5. loadText(java.io.File f)