簡體   English   中英

NotePad無法使用PrintStream輸出到文本文件來識別java代碼中的換行符

[英]NotePad not recognizing new line characters in java code using PrintStream to output to text file

我需要一些幫助學校的java項目。 請參閱下面的代碼,了解Book和BookApplication類,以及輸入文本文件和所需的輸出文本文件。

我的輸出文件有問題。 當我在Word或寫字板或NotePad ++中打開它時,文件格式正確。 但是當我在NotePad中打開文件時,報告全部都在一行上。

由於某種原因,NotePad無法識別新的行字符。 有什么想法嗎? 如何更改Java代碼以便NotePad正確顯示報告? 我是一個Java菜鳥,我在這個Java課程中真的很掙扎。 您可以提供的任何幫助將非常感激。 謝謝。

Java簡介0123444555680 Tony Gaddis TG003 Javascript簡介9780071632969 John Pollock JP402游戲開發基礎9780763778 Heather Maxwell Chandler HC026

Dean Publishers - 書籍清單

Java簡介:Tony Gaddis(TG003)ISBN:0123444555680

Javascript簡介作者:John Pollock(JP402)ISBN:9780071632969

游戲開發的基礎由Heather Maxwell Chandler(HC026)ISBN:9780763778

我們目前的目錄有3本書。

感謝您的關注。

//

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

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

您正在使用\\n字符表示新行,這對於* nix是正確的。 在Windows中它是\\r\\n 如果您不想擔心自己所處的平台,可以使用:

System.getProperty("line.separator")

獲得該令牌。 這樣你的程序就可以在任何平台上運行。

更新:在Java 7中,您可以使用System.lineSeparator()

在Windows上需要\\r\\n0x0D 0x0A )來結束一行,並且需要兩對它們才能輸出一個空行。

嘗試使用系統行分隔符(如所述,在Windows中為\\ r \\ n)

String nl = System.getProperty("line.separator")
output += book.getBookTitle() + nl;
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM