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

Last change on this file since 122 was 83, checked in by sherbold, 13 years ago
  • fixed typo
  • Property svn:mime-type set to text/plain
File size: 1.6 KB
Line 
1package de.ugoe.cs.util;
2
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.io.FileReader;
6import java.io.IOException;
7
8public class FileTools {
9       
10        /**
11         * <p>
12         * Returns an array of the lines contained in a file. The line seperator is "\r\n".
13         * </p>
14         * @param filename name of the file
15         * @return string array, where each line contains a file
16         * @throws IOException see {@link FileReader#read(char[])}, {@link FileReader#close()}
17         * @throws FileNotFoundException see {@link FileReader#FileReader(File)}
18         */
19        public static String[] getLinesFromFile(String filename) throws IOException, FileNotFoundException {
20                return getLinesFromFile(filename, true);
21        }
22       
23        /**
24         * <p>
25         * Returns an array of the lines contained in a file.
26         * </p>
27         * @param filename name of the file
28         * @param carriageReturn if true, "\r\n", if false "\n" is used as line seperator
29         * @return string array, where each line contains a file
30         * @throws IOException see {@link FileReader#read(char[])}, {@link FileReader#close()}
31         * @throws FileNotFoundException see {@link FileReader#FileReader(File)}
32         */
33        public static String[] getLinesFromFile(String filename, boolean carriageReturn) throws IOException, FileNotFoundException {
34                File f = new File(filename);
35                FileReader reader = new FileReader(f);
36                char[] buffer = new char[(int) f.length()];
37                reader.read(buffer);
38                reader.close();
39                String splitString;
40                if( carriageReturn ) {
41                        splitString = "\r\n";
42                } else {
43                        splitString = "\n";
44                }
45                return (new String(buffer)).split(splitString);
46        }
47
48}
Note: See TracBrowser for help on using the repository browser.