source: trunk/JavaHelperLib/src/de/ugoe/cs/util/FileTools.java @ 401

Last change on this file since 401 was 330, checked in by sherbold, 13 years ago
  • added private constructors to helper classes de.ugoe.cs.util.ArrayTools?, de.ugoe.cs.util.FileTools?, and de.ugoe.cs.util.StringTools? to prevent initialization of the classes (they only provide static methods)
  • Property svn:mime-type set to text/plain
File size: 2.2 KB
Line 
1package de.ugoe.cs.util;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.FileReader;
7import java.io.IOException;
8import java.io.InputStreamReader;
9import 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 */
19public 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         * "\r\n".
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                return getLinesFromFile(filename, true);
48        }
49
50        /**
51         * <p>
52         * Returns an array of the lines contained in a file.
53         * </p>
54         *
55         * @param filename
56         *            name of the file
57         * @param carriageReturn
58         *            if true, "\r\n", if false "\n" is used as line separator
59         * @return string array, where each line contains a file
60         * @throws IOException
61         *             see {@link FileReader#read(char[])},
62         *             {@link FileReader#close()}
63         * @throws FileNotFoundException
64         *             see {@link FileReader#FileReader(File)}
65         */
66        public static String[] getLinesFromFile(String filename,
67                        boolean carriageReturn) throws IOException, FileNotFoundException {
68                File f = new File(filename);
69                FileInputStream fis = new FileInputStream(f);
70                InputStreamReader reader = new InputStreamReader(fis,
71                                Charset.defaultCharset());
72                char[] buffer = new char[(int) f.length()];
73                reader.read(buffer);
74                reader.close();
75                String splitString;
76                if (carriageReturn) {
77                        splitString = "\r\n";
78                } else {
79                        splitString = "\n";
80                }
81                return (new String(buffer)).split(splitString);
82        }
83
84}
Note: See TracBrowser for help on using the repository browser.