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

Last change on this file since 316 was 310, checked in by sherbold, 13 years ago
  • now specifies character encoding of file explicitly instead of assuming default encoding
  • Property svn:mime-type set to text/plain
File size: 2.1 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         * Returns an array of the lines contained in a file. The line separator is
24         * "\r\n".
25         * </p>
26         *
27         * @param filename
28         *            name of the file
29         * @return string array, where each line contains a file
30         * @throws IOException
31         *             see {@link FileReader#read(char[])},
32         *             {@link FileReader#close()}
33         * @throws FileNotFoundException
34         *             see {@link FileReader#FileReader(File)}
35         */
36        public static String[] getLinesFromFile(String filename)
37                        throws IOException, FileNotFoundException {
38                return getLinesFromFile(filename, true);
39        }
40
41        /**
42         * <p>
43         * Returns an array of the lines contained in a file.
44         * </p>
45         *
46         * @param filename
47         *            name of the file
48         * @param carriageReturn
49         *            if true, "\r\n", if false "\n" is used as line separator
50         * @return string array, where each line contains a file
51         * @throws IOException
52         *             see {@link FileReader#read(char[])},
53         *             {@link FileReader#close()}
54         * @throws FileNotFoundException
55         *             see {@link FileReader#FileReader(File)}
56         */
57        public static String[] getLinesFromFile(String filename,
58                        boolean carriageReturn) throws IOException, FileNotFoundException {
59                File f = new File(filename);
60                FileInputStream fis = new FileInputStream(f);
61                InputStreamReader reader = new InputStreamReader(fis,
62                                Charset.defaultCharset());
63                char[] buffer = new char[(int) f.length()];
64                reader.read(buffer);
65                reader.close();
66                String splitString;
67                if (carriageReturn) {
68                        splitString = "\r\n";
69                } else {
70                        splitString = "\n";
71                }
72                return (new String(buffer)).split(splitString);
73        }
74
75}
Note: See TracBrowser for help on using the repository browser.