我正在尝试使用java 8中内置的Scanner类读取.txt文件,但是我得到的结果基本上是相同的代码行。关于我的.txt文件的一些小背景:它们都包含1000行不同的数据(一个是关于一本书及其功能,另一个是关于赞助人)。然而,当我出去打印我的列表时,Patron.txt文件打印出所有1000行,而我的Books.txt只打印出最多483行。下面我展示了我的.txt文件的483行,它看起来与其他文件类似。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class LibManager {
private ArrayList<Book> bookList = new ArrayList<Book>();
private ArrayList<Patron> patronList = new ArrayList<Patron>();
private ArrayList<Loan> loanList = new ArrayList<Loan>();
private String[] menuOptions;
public static void main(String[] args) {
LibManager lm = new LibManager();
lm.execute();
}
public LibManager() {
bookList = readBooks("Resources/books.txt");
patronList = readPatrons("Resources/patrons.txt");
loanList = readLoans("Resources/loans.txt");
menuOptions = new String[] { "Add Book", "Add Patron", "List Books", "List Patrons", "List By Author",
"List By Year", "Lend Book", "Return Book", "Show Borrower", "Show Borrowed Books", "Remove Book", "Remove Patron",
"Show Overdue Books", "Exit" };
}
private void execute() {
while (true) {
int opt = getMenuOption();
switch (opt) {
case 1:
addBook();
break;
case 2:
addPatron();
break;
case 3:
listBooks();
break;
case 4:
listPatrons();
break;
case 5:
listByAuthor();
break;
case 6:
listByYear();
break;
case 7:
lendBookToPatron();
break;
case 8:
returnBook();
break;
case 9:
showBorrowers();
break;
case 10:
showBorrowedBooks();
break;
case 11:
removeBook();
break;
case 12:
removePatron();
break;
case 13:
showOverdueBooks();
break;
case 14:
exitProgram();
break;
default:
System.out.println("No such option");
}
}
}
private int getMenuOption() {
System.out.println("Select one of the following options");
for (int i = 0; i < menuOptions.length; i++) {
System.out.println("\t" + (i + 1) + ". " + menuOptions[i]);
}
Scanner s = new Scanner(System.in);
int choice = s.nextInt();
return choice;
}
/* MAKE NO CHANGES ABOVE THIS LINE */
/* COMPLETE ALL THE CODE STUBS BELOW */
private void exitProgram() {
System.out.println("Exiting..");
System.exit(0);
}
private ArrayList<Book> readBooks(String filename) {
List lines = new ArrayList();
Scanner s = null;
File infile = new File(filename);
try {
s = new Scanner(infile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (s.hasNext())
lines.add(s.nextLine().replaceAll("\\s+",""));
return (ArrayList<Book>) lines;
}
private ArrayList<Patron> readPatrons(String filename) {
List lines = new ArrayList();
Scanner s = null;
File infile = new File(filename);
try {
s = new Scanner(infile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (s.hasNext())
lines.add(s.nextLine());
return (ArrayList<Patron>) lines;
}
readBooks的结果(最后5行): 预计B999。
B48;Choke;ChuckPalahniuk;1897
B480;Voss;PatrickWhite;1953
B481;TheMidwichCuckoos;JohnWyndham;1864
B482;BlueNoon;GeorgesBataille;1781
B483;HomoFaber;MaxFri
readPatrons的结果(最后5行): 预计P999
P995 MURILLO
P996 LUTZ
P997 DUARTE
P998 KIDD
P999 KEY
查看我的Books.txt文件,第483行没有任何问题
B483 ; Homo Faber ; Max Frisch ; 1955
更新:问题仍然存在,但我可以在列表顶部添加另一本书,使其超过483分。
用于打印图书清单。
private void listBooks() {
for(int i=0;i<bookList.size();i++){
System.out.println(bookList.get(i));
}
}
答案 0 :(得分:0)
我有些如何解决自己的问题。所以我要发布我的所作所为。我导入了FileInputStream并将其添加到我的readBooks方法中。但是,我仍然感到困惑,为什么这适用于FileInputStream而不是我的原始方法?
private ArrayList<Book> readBooks(String filename) {
ArrayList<Book> lines = new ArrayList<>();
Scanner s = null;
File infile = new File(filename);
try{
FileInputStream fis = new FileInputStream(infile);
s = new Scanner(fis);
} catch (FileNotFoundException e){
e.printStackTrace();
}
while(s.hasNextLine())
lines.add(new Book(s.nextLine()));
return lines;