我尝试从URL地址读取文本文件并拆分&将其转换为ArrayList 我可以阅读它并将其打印到控制台但不知道我应该如何将InputStream或Stream转换为Arraylist。
这就是我现在所得到的:
My Book课程。
import java.io.*;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.stream.Stream;
public class Book implements BookList {
private String title;
private String author;
private BigDecimal price;
public static void main(String[] args) {
try (InputStream is = new URL("http://www.contribe.se/bookstoredata/bookstoredata.txt").openConnection().getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
Stream<String> stream = reader.lines()) {
stream.forEach(System.out::println);
} catch (MalformedURLException | FileNotFoundException e) {
System.out.println("Could not find the file.");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IOException error.");
e.printStackTrace();
}
}
@Override
public Book[] list(String searchString) {
return new Book[0];
}
@Override
public boolean add(Book book, int quantity) {
return false;
}
@Override
public int[] buy(Book... books) {
return new int[0];
}
我的界面:
public interface BookList {
public Book[] list(String searchString);
public boolean add(Book book, int quantity);
public int[] buy(Book... books);
}