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

Last change on this file since 1142 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
  • Property svn:mime-type set to text/plain
File size: 3.0 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[])},
55         *             {@link FileReader#close()}
56         * @throws FileNotFoundException
57         *             see {@link FileReader#FileReader(File)}
58         */
59        public static String[] getLinesFromFile(String filename)
60                        throws IOException, FileNotFoundException {
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[])},
80         *             {@link FileReader#close()}
81         * @throws FileNotFoundException
82         *             see {@link FileReader#FileReader(File)}
83         */
84        public static String[] getLinesFromFile(String filename,
85                        boolean carriageReturn) throws IOException, FileNotFoundException {
86                File f = new File(filename);
87                FileInputStream fis = new FileInputStream(f);
88                InputStreamReader reader = new InputStreamReader(fis,
89                                Charset.defaultCharset());
90                char[] buffer = new char[(int) f.length()];
91                reader.read(buffer);
92                reader.close();
93                String splitString;
94                if (carriageReturn) {
95                        splitString = "\r\n";
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.