我需要一些帮助学校的java项目.请参阅下面的代码,了解Book和BookApplication类,以及输入文本文件和所需的输出文本文件.
我的输出文件有问题.当我在Word或写字板或NotePad ++中打开它时,文件格式正确.但是当我在NotePad中打开文件时,报告全部都在一行上.
由于某种原因,NotePad无法识别新的行字符.有什么想法吗?如何更改Java代码以便NotePad正确显示报告?我是一个Java菜鸟,我在这个Java课程中真的很挣扎.您可以提供的任何帮助将非常感激.谢谢.
名为inBook.txt的输入文本文件如下所示:
Java简介0123444555680 Tony Gaddis TG003 Javascript简介9780071632969 John Pollock JP402游戏开发基础9780763778 Heather Maxwell Chandler HC026
名为outBook.txt的输出文本文件需要完全按如下方式读取:
Dean Publishers - 书籍清单
Java简介:Tony Gaddis(TG003)ISBN:0123444555680
Javascript简介作者:John Pollock(JP402)ISBN:9780071632969
游戏开发的基础由Heather Maxwell Chandler(HC026)ISBN:9780763778
我们目前的目录有3本书.
感谢您的关注.
// Book.java
public class Book {
public static final String BOOK_PUBLISHER = "Dean Publishers";
private String bookTitle;
private String bookISBN;
private Author bookAuthor;
Book(String inTitle) {
setBookTitle(inTitle);
setBookISBN("0");
setBookAuthor("");
}
public void setBookTitle(String inTitle) {
bookTitle = inTitle;
}
public void setBookISBN(String inISBN) {
bookISBN = inISBN;
}
public void setBookAuthor(String inName) {
bookAuthor = new Author(inName);
}
public void setBookAuthorID(String inID) {
bookAuthor.setAuthorID(inID);
}
public String getBookTitle() {
return bookTitle;
}
public String getBookISBN() {
return bookISBN;
}
public Author getAuthor() {
return bookAuthor;
}
public String getAuthorName() {
return bookAuthor.getAuthorName();
}
public String getAuthorID() {
return bookAuthor.getAuthorID();
}
//inner class
class Author {
private String authorName; //instance property
private String authorID; //instance property
//constructor
Author(String inName) {
setAuthorName(inName);
setAuthorID("0");
}
public void setAuthorName(String inName) {
authorName = inName;
}
public void setAuthorID(String inID) {
authorID = inID;
}
public String getAuthorName() {
return authorName;
}
public String getAuthorID() {
return authorID;
}
} //end inner Author class
} //end Book class
Run Code Online (Sandbox Code Playgroud)
BookApplication.java
import java.util.*;
import java.io.*;
public class BookApplication {
public static void main(String[] args) throws IOException {
// Create Book object for each book in the file and store them in array.
Scanner filein = new Scanner(new File("inBook.txt"));
// I will use ArrayList, a dynamic-size array
ArrayList<Book> books = new ArrayList<Book>();
while(filein.hasNextLine()) {
// read book data
String title = filein.nextLine();
String isbn = filein.nextLine();
String author = filein.nextLine();
String authorID = filein.nextLine();
// create new book from input
Book temp = new Book(title);
temp.setBookISBN(isbn);
temp.setBookAuthor(author);
temp.getAuthor().setAuthorID(authorID);
// add to arraylist
books.add(temp);
}
// Create an output report by reading the Book objects from the array.
String output = "";
for(Book book : books) {
output += book.getBookTitle() + "\n";
output += "By "+book.getAuthor().getAuthorName() + " (" + book.getAuthor().getAuthorID() + ")\n";
output += "ISBN: " + book.getBookISBN() + "\n\n";
}
// Report prints to console
System.out.println(output);
// Report prints to file called outBook.txt
PrintStream fileout = new PrintStream(new File("outBook.txt"));
fileout.println(output);
fileout.close();
// Count the number of books outputted to the report.
System.out.println("Our current catalog has " + books.size() + " books.");
System.out.println("\nThank you for your interest.");
} // end main method
} // end BookApplication
Run Code Online (Sandbox Code Playgroud)
Kir*_*oll 20
您正在使用\n新行的字符,这对于*nix是正确的.在Windows中它是\r\n.如果您不想担心自己所处的平台,可以使用:
System.getProperty("line.separator")
Run Code Online (Sandbox Code Playgroud)
获得该令牌.这样你的程序就可以在任何平台上运行.
更新:在Java 7中,您可以使用System.lineSeparator().
尝试使用系统行分隔符(如所述,在Windows中为\ r \n)
String nl = System.getProperty("line.separator")
output += book.getBookTitle() + nl;
...
Run Code Online (Sandbox Code Playgroud)