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
RevLine 
[73]1package de.ugoe.cs.util;
2
3import java.io.File;
[310]4import java.io.FileInputStream;
[73]5import java.io.FileNotFoundException;
6import java.io.FileReader;
7import java.io.IOException;
[310]8import java.io.InputStreamReader;
9import 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]19public 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
33         * "\r\n".
[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 {
[73]47                return getLinesFromFile(filename, true);
48        }
[175]49
[73]50        /**
51         * <p>
52         * Returns an array of the lines contained in a file.
53         * </p>
[175]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
[73]59         * @return string array, where each line contains a file
[175]60         * @throws IOException
61         *             see {@link FileReader#read(char[])},
62         *             {@link FileReader#close()}
63         * @throws FileNotFoundException
64         *             see {@link FileReader#FileReader(File)}
[73]65         */
[175]66        public static String[] getLinesFromFile(String filename,
67                        boolean carriageReturn) throws IOException, FileNotFoundException {
[73]68                File f = new File(filename);
[310]69                FileInputStream fis = new FileInputStream(f);
70                InputStreamReader reader = new InputStreamReader(fis,
71                                Charset.defaultCharset());
[73]72                char[] buffer = new char[(int) f.length()];
73                reader.read(buffer);
74                reader.close();
75                String splitString;
[175]76                if (carriageReturn) {
[73]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.