Java Text File Read Line readLines(String filename)

Here you can find the source of readLines(String filename)

Description

Read all lines of a file.

License

Open Source License

Parameter

Parameter Description
filename name of file to read

Exception

Parameter Description
IOException on any input error

Return

array of lines read from file

Declaration

public static String[] readLines(String filename) throws IOException 

Method Source Code


//package com.java2s;
// Refer to LICENSE for terms and conditions of use.

import java.io.BufferedReader;
import java.io.File;

import java.io.FileReader;
import java.io.IOException;

import java.util.Vector;

public class Main {
    /**/*  ww  w.  j  a v  a 2 s  .c o m*/
     * Read all lines of a file.
     * 
     * @param f
     *            file to read
     * @return array of lines read from file
     * @throws IOException
     *             on any input error
     */
    public static String[] readLines(File f) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(f));
        Vector lines = new Vector();
        String line = br.readLine();
        while (line != null) {
            int commentLoc = line.indexOf('#');
            if (commentLoc != -1)
                line = line.substring(0, commentLoc);
            if (line.trim().length() > 0)
                lines.add(line);
            line = br.readLine();
        }
        br.close();
        String[] lines2 = new String[lines.size()];
        lines.copyInto(lines2);
        return lines2;
    }

    /**
     * Read all lines of a file.
     * 
     * @param filename
     *            name of file to read
     * @return array of lines read from file
     * @throws IOException
     *             on any input error
     */
    public static String[] readLines(String filename) throws IOException {
        return readLines(new File(filename));
    }
}

Related

  1. readLines(String file)
  2. readLines(String file, ArrayList lines)
  3. readLines(String filename)
  4. readLines(String fileName)
  5. readlines(String filename)
  6. readLines(String filename)
  7. readLines(String filename)
  8. readLines(String fileName)
  9. readLines(String filename)