您好。我试图从Dropbox URL读取文本文件,并将文本文件的内容放入ArrayList。 我能够通过使用openStream()方法读取和打印数据,但我似乎无法弄清楚如何将数据放入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);
会很感激帮助吗?
答案 0 :(得分:3)
List<String> list = new ArrayList<String>();
while ((inputLine = in.readLine()) != null) {
list.add(inputLine);
}
答案 1 :(得分:2)
尝试这样的事情:
String inputLine;
ArrayList<String> array = new ArrayList<String>();
while ((inputLine = in.readLine()) != null) {
array.add(inputLine);
}
答案 2 :(得分:1)
这取决于你想做什么:
将多个DropBox文件存储在ArrayList中,其中1个项目代表1个文件
使用StringBuilder
将所有线条拼接成一个字符串。
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();
}
将文件的每一行存储在ArrayList的记录中
String inputLine;
ArrayList<String> array = new ArrayList<String>();
while ((inputLine = in.readLine()) != null) {
array.add(inputLine);
}