Reputation: 4295
I have this piece of code in java
public class read{
private ArrayList <String[]> test = new ArrayList<String[]>();
//arr is a valid array
//br is a buffered reader
while(br.readline())
test.add(store(arr[]));
public String[] store (String[] str_arr) {
String a;
new_str_arr[] = new String[2];
new_str_arr[1] = str_arr[0];
new_str_arr[0] = "Header";
return new_str_arr;
}
How would i be able to convert this into clojure ?
Upvotes: 0
Views: 301
Reputation: 34800
Not sure what you are trying to accomplish. You don't store the result of br.readLine() into a String variable, so in your program nothing interesting will happen probably. From the text you provide, maybe this comes close:
(use 'clojure.java.io)
(with-open [rdr (reader "/tmp/test.txt")]
(doall (map vector (line-seq rdr))))
Input file:
123
12
123
12
123
12
123
12
123
Output:
(["123"] ["12"] ["123"] ["12"] ["123"] ["12"] ["123"] ["12"] ["123"])
The file is read line by line and is not in memory all at once. The result however is. The doall
is needed, because map
returns a lazy seq. When an element of the lazy seq would be realized outside the with-open, the file would already have been closed.
Upvotes: 0
Reputation: 29438
Not clear what you want, however, if want to read text file and store each line to an array of element, and if the file is not huge in size, you can try this:
(use '[clojure.string :only (split)])
(split (slurp "file_name.txt") #"\r\n"))
Upvotes: 3