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

Last change on this file since 311 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
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>
[175]23         * Returns an array of the lines contained in a file. The line separator is
24         * "\r\n".
[73]25         * </p>
[175]26         *
27         * @param filename
28         *            name of the file
[73]29         * @return string array, where each line contains a file
[175]30         * @throws IOException
31         *             see {@link FileReader#read(char[])},
32         *             {@link FileReader#close()}
33         * @throws FileNotFoundException
34         *             see {@link FileReader#FileReader(File)}
[73]35         */
[175]36        public static String[] getLinesFromFile(String filename)
37                        throws IOException, FileNotFoundException {
[73]38                return getLinesFromFile(filename, true);
39        }
[175]40
[73]41        /**
42         * <p>
43         * Returns an array of the lines contained in a file.
44         * </p>
[175]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
[73]50         * @return string array, where each line contains a file
[175]51         * @throws IOException
52         *             see {@link FileReader#read(char[])},
53         *             {@link FileReader#close()}
54         * @throws FileNotFoundException
55         *             see {@link FileReader#FileReader(File)}
[73]56         */
[175]57        public static String[] getLinesFromFile(String filename,
58                        boolean carriageReturn) throws IOException, FileNotFoundException {
[73]59                File f = new File(filename);
[310]60                FileInputStream fis = new FileInputStream(f);
61                InputStreamReader reader = new InputStreamReader(fis,
62                                Charset.defaultCharset());
[73]63                char[] buffer = new char[(int) f.length()];
64                reader.read(buffer);
65                reader.close();
66                String splitString;
[175]67                if (carriageReturn) {
[73]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.