从文本文件读取并存储到集合

时间:2014-08-25 12:10:47

标签: java text arraylist

我正在考虑将一堆从文本文件中读取的数据存储到arraylist集合中的方法。在我阅读文件后,我不得不知道如何存储它。例如,来自文本文件的数据: 用C ++解决问题:Walter Savitch:9780132773348:QA760-073:NON FICTION:2 这是我的代码的一部分:

从文本文件中获取:

public void actionPerformed(ActionEvent arg0) {
            JFileChooser chooser = new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter(
                    "Text and Data Files", "txt", "dat");
            chooser.setFileFilter(filter);
            int returnVal = chooser.showOpenDialog(MainMenu.this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                String filename = chooser.getSelectedFile()
                        .getAbsolutePath();
                readBooks(filename);
            }
        }


public void readBooks(String filename) {
    try {
        BufferedReader reader = new BufferedReader(new FileReader(filename));
        int i = 0;
        String line;
        line = reader.readLine();
        while (line != null && !line.equals("")) {
            i++;
            processBook(line);
            line = reader.readLine();
        }
        System.out.println("" + i + " books read");
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    }
}

public void processBook(String line) {
    int id = nextBookID;
    nextBookID++;

    String[] parts = line.split(":");
    String title = parts[0];
    String author = parts[1];
    String bookISBN = parts[2];
    String callNumber = parts[3];
    String type = parts[4];
    String noOfCopy = parts[5];     

    System.out.println("Creating book " + id);

    // TODO: add create book and add to collection(s)



}

4 个答案:

答案 0 :(得分:2)

您应该从创建书类开始:

public class Book() {
    private String title;
    private String author;
    private String bookISBN;
    private String callNumber;
    private String type;
    private String noOfCopy;

    public Book(String title,
        String author,
        String bookISBN,
        String callNumber,
        String type,
        String noOfCopy) {

        this.author = author;
        this.bookISBN = bookISBN;
        this.callNumber = callNumber;
        this.type = type;
        this.noOfCopy = noOfCopy;
    }
}

您现在有一个课程类,您可以将其存储在一个数组的书籍列表中。在readBooks方法中,您应该创建此数组列表:

ArrayList<Book> bookList = new ArrayList<>();

现在您在processBook方法中使用此列表。使用我定义的构造函数erlier创建Book对象:

Book book = new Book(title, author, bookISBN, callNumber, type, noOfCopy);

您现在可以将此书添加到您的arrayList:

bookList.add(book);

这会将您的图书存储在一个集合中。

答案 1 :(得分:1)

我会创建一个类。例如:

public class Book{
    private int id;
    private String title;
    private String author;
    private String ISBN;
    private String callNumber;
    private String type;
    private String noOfCopy;

    public Book(int id, String title, String author, String ISBN, String callNumber, String type, String noOfCopy) {
        this.id = id;
        this.title = title;
        this.author = author;
        this.ISBN = ISBN;
        this.callNumber = callNumber;
        this.type = type;
        this.noOfCopy = noOfCopy;
    }
    <Getter and Setter>
}

并将新的Bookinstance添加到Arraylist。

List<Book> books= new ArrayList<Book>();
books.add(new Book(id, parts[0], parts[1], parts[2], parts[3], parts[4], parts[5]))

答案 2 :(得分:0)

我认为最好的方法是创建一个Book类,如下所示:

public class Book {

    private String title;
    private String author;
    private String bookISBN;
    private String callNumber;
    private String type;
    private long noOfCopy;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getBookISBN() {
        return bookISBN;
    }

    public void setBookISBN(String bookISBN) {
        this.bookISBN = bookISBN;
    }

    public String getCallNumber() {
        return callNumber;
    }

    public void setCallNumber(String callNumber) {
        this.callNumber = callNumber;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public long getNoOfCopy() {
        return noOfCopy;
    }

    public void setNoOfCopy(long noOfCopy) {
        this.noOfCopy = noOfCopy;
    }
}

然后只需在processBook

创建新对象
Book tempBook = new Book();
tempBook.setTitle(title);
...

并将其添加到List,例如:

List<Book> myList = new ArrayList<>();
...
myList.add(tempBook);

答案 3 :(得分:0)

可以在不使用类分层代码的情况下完成,只需使用Container对象(如字符串向量)作为第二个参数传递给readBooks函数和其他函数。

但是使用class是更好的选择,它更模块化,可维护和可读