source: trunk/java-utils/src/main/java/de/ugoe/cs/util/FileTools.java @ 1242

Last change on this file since 1242 was 1240, checked in by pharms, 13 years ago
  • applied code formating
  • Property svn:mime-type set to text/plain
File size: 3.2 KB
Line 
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
15package de.ugoe.cs.util;
16
17import java.io.File;
18import java.io.FileInputStream;
19import java.io.FileNotFoundException;
20import java.io.FileReader;
21import java.io.IOException;
22import java.io.InputStreamReader;
23import java.nio.charset.Charset;
24
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 */
33public class FileTools {
34
35    /**
36     * <p>
37     * Private constructor to prevent initializing of the class.
38     * </p>
39     */
40    private FileTools() {
41
42    }
43
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    }
67
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    }
101
102}
Note: See TracBrowser for help on using the repository browser.