【问题标题】:Read from text file and store to collection从文本文件读取并存储到集合
【发布时间】:2014-08-25 12:10:47
【问题描述】:

我正在考虑一种方法将从文本文件中读取的一堆数据存储到一个数组列表中。在我阅读文件后,我必须知道如何存储它。例如,来自文本文件的数据: 用 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)



}

【问题讨论】:

  • 收藏什么??字符串??
  • 可能我想设置一个数组列表

标签: java text arraylist


【解决方案1】:

你应该从创建书籍类开始:

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 方法中使用此列表。您使用我定义的构造函数创建 Book 对象:

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

您现在可以将这本书添加到您的数组列表中:

bookList.add(book);

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

【讨论】:

  • 我可以将arraylist声明为全局变量并可以通过其他方法访问吗?
  • 您可以将其声明为全局验证并在其他方法中访问它是的。这是另一种可能性。
  • 好的,我解决了,谢谢。与使用 arraylist 的树集相比,哪一种是存储数据集合的推荐方式?
  • 与集合相反,列表是有序的。如果您需要订购书籍,您应该使用 ArrayList。否则没关系。
【解决方案2】:

我会创建一个类。例如:

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]))

【讨论】:

    【解决方案3】:

    我认为最好的方法是创建一个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);
    

    【讨论】:

      【解决方案4】:

      无需将代码与类分层即可完成,您只需使用容器对象(如字符串向量)作为第二个参数传递给 readBooks 函数和其他函数..

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多