Java BufferedReader Read readFile(File f, String newLineChar)

Here you can find the source of readFile(File f, String newLineChar)

Description

Reads contents of file (f) into a String (up to five megs) using newLineChar as a line seperator

License

Apache License

Declaration

public static String readFile(File f, String newLineChar) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    /**/*from   ww  w.j a  v  a  2 s  . com*/
     * Reads contents of file (f) into a String (up to five megs)
     */
    public static String readFile(File f) throws IOException {
        return readFile(f, "\n");
    }

    public static String readFile(File f, int startLine, int numRowsToRead) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(f));
        for (int i = 1; i < startLine; i++) {
            reader.readLine();
        }
        StringBuilder rtn = new StringBuilder();

        counter: for (int i = 0; i < numRowsToRead; i++) {
            String line = reader.readLine();
            if (line != null) {
                rtn.append(line);
            } else {
                break counter;
            }
        }

        reader.close();
        return rtn.toString();

    }

    /**
     * Reads contents of file (f) into a String (up to five megs) using newLineChar as a line seperator
     */
    public static String readFile(File f, String newLineChar) throws IOException {
        if (f.length() > 5000000)
            return "File too long";
        BufferedReader buf = new BufferedReader(new FileReader(f));
        StringBuffer results = new StringBuffer();
        while (buf.ready()) {
            results.append(buf.readLine()).append(newLineChar);
        }
        return results.toString();
    }
}

Related

  1. readFile(File f)
  2. readFile(File f)
  3. readFile(File f)
  4. ReadFile(File f, boolean keep_newline)
  5. readFile(File f, boolean preserveLineBreaks)
  6. readFile(File file)
  7. readFile(File file)
  8. readFile(File file)
  9. readFile(File file)