Skip to content

Instantly share code, notes, and snippets.

@mchirico
Created January 29, 2013 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mchirico/4665704 to your computer and use it in GitHub Desktop.
Save mchirico/4665704 to your computer and use it in GitHub Desktop.
Example FileRead in java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class FileRead {
public String[] readLines(String filename) throws IOException {
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
List<String> lines = new ArrayList<String>();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
lines.add(line);
}
bufferedReader.close();
return lines.toArray(new String[lines.size()]);
}
public static void main(String[] args) {
FileRead fap = new FileRead();
try {
String[] lines = fap.readLines("/Users/mchirico/testData/data");
for (String line : lines) {
System.out.println(line);
}
} catch (Exception e) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment