• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.unicode.cldr.util;
2 
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.nio.charset.Charset;
6 import java.nio.charset.StandardCharsets;
7 
8 import org.unicode.cldr.util.With.SimpleIterator;
9 
10 import com.ibm.icu.util.ICUUncheckedIOException;
11 
12 /**
13  * Class to get an Iterable for Strings from a File, returning one line at a time.
14  * @author ribnitz
15  *
16  */
17 public class StringIterables {
18     /**
19      * Simple API to iterate over file lines. Example:
20      * for (String s : FileUtilities.in(directory,name)) {
21      * ...
22      * }
23      *
24      * @author markdavis
25      *
26      */
in(Class<?> class1, String file)27     public static Iterable<String> in(Class<?> class1, String file) {
28         return With.in(new FileLines(FileReaders.openFile(class1, file, StandardCharsets.UTF_8)));
29     }
30 
31     /**
32      * Simple API to iterate over file lines. Example:
33      * for (String s : FileUtilities.in(directory,name)) {
34      * ...
35      * }
36      *
37      * @author markdavis
38      *
39      */
in(Class<?> class1, String file, Charset charset)40     public static Iterable<String> in(Class<?> class1, String file, Charset charset) {
41         return With.in(new FileLines(FileReaders.openFile(class1, file, charset)));
42     }
43 
44     /**
45      * Simple API to iterate over file lines. Example:
46      * for (String s : FileUtilities.in(directory,name)) {
47      * ...
48      * }
49      *
50      * @author markdavis
51      *
52      */
in(String directory, String file)53     public static Iterable<String> in(String directory, String file) {
54         return With.in(new FileLines(FileReaders.openFile(directory, file, StandardCharsets.UTF_8)));
55     }
56 
57     /**
58      * Simple API to iterate over file lines. Example:
59      * for (String s : FileUtilities.in(directory,name)) {
60      * ...
61      * }
62      *
63      * @author markdavis
64      *
65      */
in(BufferedReader reader)66     public static Iterable<String> in(BufferedReader reader) {
67         return With.in(new FileLines(reader));
68     }
69 
70     /**
71      * Simple API to iterate over file lines. Example:
72      * for (String s : FileUtilities.in(directory,name)) {
73      * ...
74      * }
75      *
76      * @author markdavis
77      *
78      */
in(String directory, String file, Charset charset)79     public static Iterable<String> in(String directory, String file, Charset charset) {
80         return With.in(new FileLines(FileReaders.openFile(directory, file, charset)));
81     }
82 
83     private static class FileLines implements SimpleIterator<String> {
84         private BufferedReader input;
85 
FileLines(BufferedReader input)86         public FileLines(BufferedReader input) {
87             this.input = input;
88         }
89 
90         @Override
next()91         public String next() {
92             try {
93                 String result = input.readLine();
94                 if (result == null) {
95                     input.close();
96                 }
97                 return result;
98             } catch (IOException e) {
99                 throw new ICUUncheckedIOException(e); // handle dang'd checked exception
100             }
101         }
102 
103     }
104 }