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