1 | package de.ugoe.cs.util;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileNotFoundException;
|
---|
5 | import java.io.FileReader;
|
---|
6 | import java.io.IOException;
|
---|
7 |
|
---|
8 | public class FileTools {
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * <p>
|
---|
12 | * Returns an array of the lines contained in a file. The line seperator is "\r\n".
|
---|
13 | * </p>
|
---|
14 | * @param filename name of the file
|
---|
15 | * @return string array, where each line contains a file
|
---|
16 | * @throws IOException see {@link FileReader#read(char[])}, {@link FileReader#close()}
|
---|
17 | * @throws FileNotFoundException see {@link FileReader#FileReader(File)}
|
---|
18 | */
|
---|
19 | public static String[] getLinesFromFile(String filename) throws IOException, FileNotFoundException {
|
---|
20 | return getLinesFromFile(filename, true);
|
---|
21 | }
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * <p>
|
---|
25 | * Returns an array of the lines contained in a file.
|
---|
26 | * </p>
|
---|
27 | * @param filename name of the file
|
---|
28 | * @param carriageReturn if true, "\r\n", if false "\n" is used as line seperator
|
---|
29 | * @return string array, where each line contains a file
|
---|
30 | * @throws IOException see {@link FileReader#read(char[])}, {@link FileReader#close()}
|
---|
31 | * @throws FileNotFoundException see {@link FileReader#FileReader(File)}
|
---|
32 | */
|
---|
33 | public static String[] getLinesFromFile(String filename, boolean carriageReturn) throws IOException, FileNotFoundException {
|
---|
34 | File f = new File(filename);
|
---|
35 | FileReader reader = new FileReader(f);
|
---|
36 | char[] buffer = new char[(int) f.length()];
|
---|
37 | reader.read(buffer);
|
---|
38 | reader.close();
|
---|
39 | String splitString;
|
---|
40 | if( carriageReturn ) {
|
---|
41 | splitString = "\r\n";
|
---|
42 | } else {
|
---|
43 | splitString = "\n";
|
---|
44 | }
|
---|
45 | return (new String(buffer)).split(splitString);
|
---|
46 | }
|
---|
47 |
|
---|
48 | }
|
---|