Here you can find the source of readLines(String filename)
Parameter | Description |
---|---|
filename | name of file to read |
Parameter | Description |
---|---|
IOException | on any input error |
public static String[] readLines(String filename) throws IOException
//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)); } }