读取并循环遍历由制表符分隔的文件中的所有内容,并将所有单词放入 ArrayList

问题描述 投票:0回答:1

我正在为学校做一个 java 项目,我想读取文本文件中的所有单词并将其存储在 Arraylist 中。这是文本文件的一个小样本:

1876    Gore    Vidal   Random House    4/11/1976   Fiction
23337   Stephen King    Scribner    11/27/2011  Fiction
...and Ladies of the Club   Helen Hooven    Santmeyer   Putnam  7/8/1984    Fiction
1st to Die  James   Patterson   Little, Brown   3/25/2001   Fiction
2nd Chance  James   Patterson   Little, Brown   3/24/2002   Fiction
3rd Degree  James   Patterson   Little, Brown   3/21/2004   Fiction
4th of July James   Patterson   Little, Brown   5/22/2005   Fiction

字段之间有制表符。 我的代码只读了第一行而不是完整列表。

这是我的代码:

public static void main(String[] args) throws Exception {
    try {
        FileInputStream fstream = new FileInputStream("bestsellers.txt");
        BufferedReader buf = new BufferedReader(new InputStreamReader(fstream));
        ArrayList<String> words = new ArrayList<>();
        String lineJustFetched = null;
        String[] wordsArray;
        Book CurrentBook = new Book();
        while (true) {
            lineJustFetched = buf.readLine();
            if (lineJustFetched == null) {
                break;
            } else {
                wordsArray = lineJustFetched.split("\t");
                for (String each : wordsArray) {
                    if (!"".equals(each)) {
                        words.add(each);
                    }
                }
            }
        }
        CurrentBook.title = words.get(0);
        CurrentBook.authorFirstName = words.get(1);
        CurrentBook.authorLastName = words.get(2);
        CurrentBook.publisher = words.get(3);
        CurrentBook.category = words.get(5);
        CurrentBook.date = words.get(4);
        buf.close();
        System.out.println("Author's first name is: " + CurrentBook.authorFirstName);
        System.out.println("Author's last name is: " + CurrentBook.authorLastName);
        System.out.println("Title is: " + CurrentBook.title);
        System.out.println("The publisher is: " + CurrentBook.publisher);
        System.out.println("The category is: " + CurrentBook.category);
        System.out.println("The date is: " + CurrentBook.date);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

这是输出:

Author's first name is: Gore
Author's last name is: Vidal
Title is: 1876
The publisher is: Random House
The category is: Fiction
The date is: 4/11/1976

如您所见,它只读取、存储和打印第一本书,但我想对 txt 文件中的所有单词执行此操作。

java file-io tabs
1个回答
0
投票

它存储所有的书,但你只打印一本。不要使用

Book.String = words.get[i]
分配变量,而是使用在 Book 类中为您分配变量的构造函数,例如:

public Book(String title, String authorFirstName, String authorLastName, String publisher, String category, String data) {
this.title = title;
this.authorFirstName = authorFirstName;
this.authorLastName = authorFirstName;
this.publisher = publisher;
this.category = category;
this.data = data;
}

现在在您的主要方法中将

Book() CurrentBook = new Book();
替换为
ArrayList<Book> books = new ArrayList<>();
,这就是有趣的地方。 删除
ArrayList<String> words
,你不再需要它了,并将你的while循环更改为下面的那个。

while (!(buf.readLine() == null) {
            lineJustFetched = buf.readLine();
             wordsArray = lineJustFetched.split("\t");
                books.add(new Book(wordsArray[0], wordsArray[1], wordsArray[2], wordsArray[3], wordsArray[4], wordsArray[5]));
            }

在你所有改革后的主要是

 try {
        FileInputStream fstream = new FileInputStream("bestsellers.txt");
        BufferedReader buf = new BufferedReader(new InputStreamReader(fstream));
        ArrayList<Book> books = new ArrayList<>();
        while (!(buf.readLine() == null) {
            String lineJustFetched = buf.readLine();
             String[] wordsArray = lineJustFetched.split("\t");
                books.add(new Book(wordsArray[0], wordsArray[1], wordsArray[2], wordsArray[3], wordsArray[4], wordsArray[5]));
            }
        buf.close();
       for (int i = 0; i < books.size; i++) {
        System.out.println("Author's first name is: " + books.get(i).authorFirstName);
        System.out.println("Author's last name is: " + books.get(i).authorLastName);
        System.out.println("Title is: " + books.get(i).title);
        System.out.println("The publisher is: " + books.get(i).publisher);
        System.out.println("The category is: " + books.get(i).category);
        System.out.println("The date is: " + books.get(i).date);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

这是您想要使用的方法来尊重您的代码,但是由于您为书籍使用了一个对象,我假设您稍后想要集成一个查找,因此您将制作一个 HashMap 并存储您的数据以备后用线。

public static void main(String[] args) throws Exception {
        FileInputStream fstream = new FileInputStream("bestsellers.txt");
        BufferedReader buf = new BufferedReader(new InputStreamReader(fstream));
        ArrayList<Book> books = new ArrayList<>();
        HashMap<String, Book> bookCollectiion = new HashMap<>();
        while (!(buf.readLine() == null) {
            String lineJustFetched = buf.readLine();
             String[] wordsArray = lineJustFetched.split("\t");
                books.add(new Book(wordsArray[0], wordsArray[1], wordsArray[2], wordsArray[3], wordsArray[4], wordsArray[5]));
            }
        buf.close();
       for (int i = 0; i < books.size; i++) {
        bookCollection.put(books.get(i).title, books.get(i));
        System.out.println("Author's first name is: " + books.get(i).authorFirstName);
        System.out.println("Author's last name is: " + books.get(i).authorLastName);
        System.out.println("Title is: " + books.get(i).title);
        System.out.println("The publisher is: " + books.get(i).publisher);
        System.out.println("The category is: " + books.get(i).category);
        System.out.println("The date is: " + books.get(i).date);
     // do something with the HashMapped books, maybe use a scanner for a book lookup.
}

奖金,考虑到您说过要将所有书籍存储在 ArrayList 中,我不确定您是否想要这个,但是您可以使用这个 main 完全放弃所有额外代码和 Book 类:

public static void main(String[] args) throws Exception {
FileInputStream fstream = new FileInputStream("bestsellers.txt");
        BufferedReader buf = new BufferedReader(new InputStreamReader(fstream));
      while (!(buf.readLine() == null) {
            lineJustFetched = buf.readLine();
             wordsArray = lineJustFetched.split("\t");
              System.out.println("Author's first name is: " + wordsArray[0]);
        System.out.println("Author's last name is: " + wordsArray[1]);
        System.out.println("Title is: " + wordsArray[2]);
        System.out.println("The publisher is: " + wordsArray[3]);
        System.out.println("The category is: " + wordsArray[5]);
        System.out.println("The date is: " + wordsArray[4]);
            }
}

它所做的只是打印语句,但这应该有效。

© www.soinside.com 2019 - 2024. All rights reserved.