[927] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
| 2 | //
|
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 4 | // you may not use this file except in compliance with the License.
|
---|
| 5 | // You may obtain a copy of the License at
|
---|
| 6 | //
|
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 8 | //
|
---|
| 9 | // Unless required by applicable law or agreed to in writing, software
|
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 12 | // See the License for the specific language governing permissions and
|
---|
| 13 | // limitations under the License.
|
---|
| 14 |
|
---|
[73] | 15 | package de.ugoe.cs.util;
|
---|
| 16 |
|
---|
| 17 | import java.io.File;
|
---|
[310] | 18 | import java.io.FileInputStream;
|
---|
[73] | 19 | import java.io.FileNotFoundException;
|
---|
| 20 | import java.io.FileReader;
|
---|
| 21 | import java.io.IOException;
|
---|
[310] | 22 | import java.io.InputStreamReader;
|
---|
| 23 | import java.nio.charset.Charset;
|
---|
[73] | 24 |
|
---|
[175] | 25 | /**
|
---|
| 26 | * <p>
|
---|
| 27 | * Helper class that provides methods that simplify working with files.
|
---|
| 28 | * </p>
|
---|
| 29 | *
|
---|
| 30 | * @author Steffen Herbold
|
---|
| 31 | * @version 1.0
|
---|
| 32 | */
|
---|
[73] | 33 | public class FileTools {
|
---|
[175] | 34 |
|
---|
[1240] | 35 | /**
|
---|
| 36 | * <p>
|
---|
| 37 | * Private constructor to prevent initializing of the class.
|
---|
| 38 | * </p>
|
---|
| 39 | */
|
---|
| 40 | private FileTools() {
|
---|
[330] | 41 |
|
---|
[1240] | 42 | }
|
---|
[330] | 43 |
|
---|
[1240] | 44 | /**
|
---|
| 45 | * <p>
|
---|
| 46 | * Returns an array of the lines contained in a file. The line separator is
|
---|
| 47 | * {@link StringTools#ENDLINE}.
|
---|
| 48 | * </p>
|
---|
| 49 | *
|
---|
| 50 | * @param filename
|
---|
| 51 | * name of the file
|
---|
| 52 | * @return string array, where each line contains a file
|
---|
| 53 | * @throws IOException
|
---|
| 54 | * see {@link FileReader#read(char[])}, {@link FileReader#close()}
|
---|
| 55 | * @throws FileNotFoundException
|
---|
| 56 | * see {@link FileReader#FileReader(File)}
|
---|
| 57 | */
|
---|
| 58 | public static String[] getLinesFromFile(String filename) throws IOException,
|
---|
| 59 | FileNotFoundException
|
---|
| 60 | {
|
---|
| 61 | boolean carriageReturn = true;
|
---|
| 62 | if (StringTools.ENDLINE.equals("\n")) {
|
---|
| 63 | carriageReturn = false;
|
---|
| 64 | }
|
---|
| 65 | return getLinesFromFile(filename, carriageReturn);
|
---|
| 66 | }
|
---|
[175] | 67 |
|
---|
[1240] | 68 | /**
|
---|
| 69 | * <p>
|
---|
| 70 | * Returns an array of the lines contained in a file.
|
---|
| 71 | * </p>
|
---|
| 72 | *
|
---|
| 73 | * @param filename
|
---|
| 74 | * name of the file
|
---|
| 75 | * @param carriageReturn
|
---|
| 76 | * if true, "\r\n", if false "\n" is used as line separator
|
---|
| 77 | * @return string array, where each line contains a file
|
---|
| 78 | * @throws IOException
|
---|
| 79 | * see {@link FileReader#read(char[])}, {@link FileReader#close()}
|
---|
| 80 | * @throws FileNotFoundException
|
---|
| 81 | * see {@link FileReader#FileReader(File)}
|
---|
| 82 | */
|
---|
| 83 | public static String[] getLinesFromFile(String filename, boolean carriageReturn)
|
---|
| 84 | throws IOException, FileNotFoundException
|
---|
| 85 | {
|
---|
| 86 | File f = new File(filename);
|
---|
| 87 | FileInputStream fis = new FileInputStream(f);
|
---|
| 88 | InputStreamReader reader = new InputStreamReader(fis, Charset.defaultCharset());
|
---|
| 89 | char[] buffer = new char[(int) f.length()];
|
---|
| 90 | reader.read(buffer);
|
---|
| 91 | reader.close();
|
---|
| 92 | String splitString;
|
---|
| 93 | if (carriageReturn) {
|
---|
| 94 | splitString = "\r\n";
|
---|
| 95 | }
|
---|
| 96 | else {
|
---|
| 97 | splitString = "\n";
|
---|
| 98 | }
|
---|
| 99 | return (new String(buffer)).split(splitString);
|
---|
| 100 | }
|
---|
[1243] | 101 |
|
---|
| 102 | /**
|
---|
| 103 | * <p>
|
---|
| 104 | * Autocompletes a give path. The path must be absolute. Otherwise, autocompletion
|
---|
| 105 | * is not possible.
|
---|
| 106 | * </p>
|
---|
| 107 | *
|
---|
| 108 | * @param prefix
|
---|
| 109 | * the prefix to be complete
|
---|
| 110 | * @return the auto completed path
|
---|
| 111 | */
|
---|
| 112 | public static String autoCompletePath(String prefix) {
|
---|
| 113 | File prefixFile = new File(prefix);
|
---|
| 114 | File parentDir = prefixFile.getParentFile();
|
---|
| 115 |
|
---|
| 116 | if (parentDir == null) {
|
---|
| 117 | // the prefix does not denote a path or denotes one of the root directories.
|
---|
| 118 | // this can not be auto completed
|
---|
| 119 | return prefix;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | String[] completions = null;
|
---|
| 123 |
|
---|
| 124 | if (parentDir.exists()) {
|
---|
| 125 | completions = parentDir.list();
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | if (completions == null) {
|
---|
| 129 | completions = new String[0];
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | String completedPrefix;
|
---|
| 133 |
|
---|
| 134 | completedPrefix = StringTools.autocomplete(prefixFile.getName(), completions);
|
---|
| 135 |
|
---|
| 136 | File completedFile = new File(parentDir, completedPrefix);
|
---|
| 137 |
|
---|
| 138 | if (completedFile.exists() && completedFile.isDirectory()) {
|
---|
| 139 | return completedFile.getAbsolutePath() + File.separator;
|
---|
| 140 | }
|
---|
| 141 | else {
|
---|
| 142 | return (parentDir.getAbsolutePath() + File.separator + completedPrefix)
|
---|
| 143 | .replaceAll("//", "/");
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
[73] | 146 |
|
---|
| 147 | }
|
---|