Changeset 1240
- Timestamp:
- 06/28/13 12:11:32 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/java-utils/src/main/java/de/ugoe/cs/util/FileTools.java
r927 r1240 33 33 public class FileTools { 34 34 35 36 37 38 39 40 35 /** 36 * <p> 37 * Private constructor to prevent initializing of the class. 38 * </p> 39 */ 40 private FileTools() { 41 41 42 42 } 43 43 44 45 46 47 48 49 50 51 52 53 54 * see {@link FileReader#read(char[])}, 55 * {@link FileReader#close()} 56 * @throws FileNotFoundException 57 * see {@link FileReader#FileReader(File)} 58 */ 59 public static String[] getLinesFromFile(String filename) 60 throws IOException, FileNotFoundException{61 62 if( StringTools.ENDLINE.equals("\n")) {63 64 65 66 44 /** 45 * <p> 46 * Returns an array of the lines contained in a file. The line separator is 47 * {@link StringTools#ENDLINE}. 48 * </p> 49 * 50 * @param filename 51 * name of the file 52 * @return string array, where each line contains a file 53 * @throws IOException 54 * see {@link FileReader#read(char[])}, {@link FileReader#close()} 55 * @throws FileNotFoundException 56 * see {@link FileReader#FileReader(File)} 57 */ 58 public static String[] getLinesFromFile(String filename) throws IOException, 59 FileNotFoundException 60 { 61 boolean carriageReturn = true; 62 if (StringTools.ENDLINE.equals("\n")) { 63 carriageReturn = false; 64 } 65 return getLinesFromFile(filename, carriageReturn); 66 } 67 67 68 69 70 71 72 73 74 75 76 77 78 79 * see {@link FileReader#read(char[])}, 80 * {@link FileReader#close()} 81 * @throws FileNotFoundException 82 * see {@link FileReader#FileReader(File)} 83 */ 84 public static String[] getLinesFromFile(String filename, 85 boolean carriageReturn) throws IOException, FileNotFoundException{86 87 88 InputStreamReader reader = new InputStreamReader(fis, 89 Charset.defaultCharset());90 char[] buffer = new char[(int) f.length()];91 reader.read(buffer);92 reader.close();93 String splitString; 94 if (carriageReturn) { 95 splitString = "\r\n"; 96 }else {97 98 99 100 68 /** 69 * <p> 70 * Returns an array of the lines contained in a file. 71 * </p> 72 * 73 * @param filename 74 * name of the file 75 * @param carriageReturn 76 * if true, "\r\n", if false "\n" is used as line separator 77 * @return string array, where each line contains a file 78 * @throws IOException 79 * see {@link FileReader#read(char[])}, {@link FileReader#close()} 80 * @throws FileNotFoundException 81 * see {@link FileReader#FileReader(File)} 82 */ 83 public static String[] getLinesFromFile(String filename, boolean carriageReturn) 84 throws IOException, FileNotFoundException 85 { 86 File f = new File(filename); 87 FileInputStream fis = new FileInputStream(f); 88 InputStreamReader reader = new InputStreamReader(fis, Charset.defaultCharset()); 89 char[] buffer = new char[(int) f.length()]; 90 reader.read(buffer); 91 reader.close(); 92 String splitString; 93 if (carriageReturn) { 94 splitString = "\r\n"; 95 } 96 else { 97 splitString = "\n"; 98 } 99 return (new String(buffer)).split(splitString); 100 } 101 101 102 102 }
Note: See TracChangeset
for help on using the changeset viewer.