Hi. I am trying to read a text file from a dropbox URL and put the contents of the text file to the ArrayList. I was able to read and out print the data by using openStream() method but I can't seem to be able to figure out how to put that data into an ArrayList
URL pList = new URL("http://url");
BufferedReader in = new BufferedReader(
new InputStreamReader(
pList.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
would appreciate the help ?
List<String> list = new ArrayList<String>();
while ((inputLine = in.readLine()) != null) {
list.add(inputLine);
}
Try something like this:
String inputLine;
ArrayList<String> array = new ArrayList<String>();
while ((inputLine = in.readLine()) != null) {
array.add(inputLine);
}
It depends on what you want to do:
Store multiple DropBox files in an ArrayList, where 1 item represents 1 file
Use a StringBuilder
to stitch all the lines together to one string.
List<String> files = new ArrayList<String>(); files.add(readFileUsingStringBuilder(pList)); public static String readFileUsingStringBuilder(URL url) { StringBuilder sb = new StringBuilder(); String separator = ""; BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); String line; while ((line = br.readLine() != null) { sb.append(separator); sb.append(line); separator = "\\n"; } return sb.toString(); }
Store each line of the file in a record of the ArrayList
String inputLine; ArrayList<String> array = new ArrayList<String>(); while ((inputLine = in.readLine()) != null) { array.add(inputLine); }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.