1 | package de.ugoe.cs.util;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileInputStream;
|
---|
5 | import java.io.FileNotFoundException;
|
---|
6 | import java.io.FileReader;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStreamReader;
|
---|
9 | import java.nio.charset.Charset;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * <p>
|
---|
13 | * Helper class that provides methods that simplify working with files.
|
---|
14 | * </p>
|
---|
15 | *
|
---|
16 | * @author Steffen Herbold
|
---|
17 | * @version 1.0
|
---|
18 | */
|
---|
19 | public class FileTools {
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * <p>
|
---|
23 | * Private constructor to prevent initializing of the class.
|
---|
24 | * </p>
|
---|
25 | */
|
---|
26 | private FileTools() {
|
---|
27 |
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * <p>
|
---|
32 | * Returns an array of the lines contained in a file. The line separator is
|
---|
33 | * {@link StringTools#ENDLINE}.
|
---|
34 | * </p>
|
---|
35 | *
|
---|
36 | * @param filename
|
---|
37 | * name of the file
|
---|
38 | * @return string array, where each line contains a file
|
---|
39 | * @throws IOException
|
---|
40 | * see {@link FileReader#read(char[])},
|
---|
41 | * {@link FileReader#close()}
|
---|
42 | * @throws FileNotFoundException
|
---|
43 | * see {@link FileReader#FileReader(File)}
|
---|
44 | */
|
---|
45 | public static String[] getLinesFromFile(String filename)
|
---|
46 | throws IOException, FileNotFoundException {
|
---|
47 | boolean carriageReturn = true;
|
---|
48 | if( StringTools.ENDLINE.equals("\n") ) {
|
---|
49 | carriageReturn = false;
|
---|
50 | }
|
---|
51 | return getLinesFromFile(filename, carriageReturn);
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * <p>
|
---|
56 | * Returns an array of the lines contained in a file.
|
---|
57 | * </p>
|
---|
58 | *
|
---|
59 | * @param filename
|
---|
60 | * name of the file
|
---|
61 | * @param carriageReturn
|
---|
62 | * if true, "\r\n", if false "\n" is used as line separator
|
---|
63 | * @return string array, where each line contains a file
|
---|
64 | * @throws IOException
|
---|
65 | * see {@link FileReader#read(char[])},
|
---|
66 | * {@link FileReader#close()}
|
---|
67 | * @throws FileNotFoundException
|
---|
68 | * see {@link FileReader#FileReader(File)}
|
---|
69 | */
|
---|
70 | public static String[] getLinesFromFile(String filename,
|
---|
71 | boolean carriageReturn) throws IOException, FileNotFoundException {
|
---|
72 | File f = new File(filename);
|
---|
73 | FileInputStream fis = new FileInputStream(f);
|
---|
74 | InputStreamReader reader = new InputStreamReader(fis,
|
---|
75 | Charset.defaultCharset());
|
---|
76 | char[] buffer = new char[(int) f.length()];
|
---|
77 | reader.read(buffer);
|
---|
78 | reader.close();
|
---|
79 | String splitString;
|
---|
80 | if (carriageReturn) {
|
---|
81 | splitString = "\r\n";
|
---|
82 | } else {
|
---|
83 | splitString = "\n";
|
---|
84 | }
|
---|
85 | return (new String(buffer)).split(splitString);
|
---|
86 | }
|
---|
87 |
|
---|
88 | }
|
---|