NotePad无法使用PrintStream输出到文本文件来识别java代码中的换行符 [英] NotePad not recognizing new line characters in java code using PrintStream to output to text file

查看:109
本文介绍了NotePad无法使用PrintStream输出到文本文件来识别java代码中的换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的学校java项目需要一些帮助。请参阅下面的代码,了解Book和BookApplication类,以及输入文本文件和所需的输出文本文件。

I need some help with my java project for school. Please see the code below for the classes Book and BookApplication, as well as the input text file and desired output text file.

我的输出文件有问题。当我在Word或写字板或NotePad ++中打开它时,文件格式正确。但是当我在NotePad中打开文件时,报告全部都在一行上。

I'm having an issue with the output file. When I open it in Word or WordPad or NotePad++, the file is formatted correctly. But when I open the file in NotePad, the report is all on one line.

由于某种原因,NotePad无法识别新行字符。有什么想法吗?如何更改Java代码以便NotePad正确显示报告?我是一个Java菜鸟,我在这个Java课程中真的很挣扎。您可以提供的任何帮助将非常感激。谢谢。

For some reason NotePad is not recognizing the new line characters. Any thoughts on this? How can I change the java code so that NotePad displays the report correctly? I am a Java noob and I'm really struggling in this Java course. Any help you can offer would be greatly apprecited. Thanks.

名为inBook.txt的输入文本文件内容如下:

Java简介
0123444555680
Tony Gaddis
TG003
Javascript简介
9780071632969
John Pollock
JP402
游戏基础知识开发
9780763778
Heather Maxwell Chandler
HC026

Intro to Java 0123444555680 Tony Gaddis TG003 Intro to Javascript 9780071632969 John Pollock JP402 Fundamentals of Game Development 9780763778 Heather Maxwell Chandler HC026

名为outBook.txt的输出文本文件需要读取完全如下:

Dean Publishers - 图书清单

Dean Publishers - List of Books

简介Java
作者:Tony Gaddis(TG003)
ISBN:0123444555680

Intro to Java By Tony Gaddis (TG003) ISBN: 0123444555680

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

Intro to Javascript By John Pollock (JP402) ISBN: 9780071632969

游戏开发基础
由Heather Maxwell Chandler(HC026)
ISBN:9780763778

Fundamentals of Game Development By Heather Maxwell Chandler (HC026) ISBN: 9780763778

我们目前的目录有3本书。

Our current catalog has 3 books.

感谢您的关注。

// 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

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


推荐答案

您正在为新行使用 \ n 字符,这对于* nix是正确的。在Windows中,它是 \\\\ n 。如果您不想担心自己所处的平台,可以使用:

You are using the \n character for new lines, which is correct for *nix. In windows it's \r\n. If you don't want to worry about what platform you're on, you can use:

System.getProperty("line.separator")

获取该令牌。这样你的程序就可以在任何平台上运行。

To get that token. That way your program will work on any platform.

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

Update: It looks like in Java 7 you can just use System.lineSeparator().

这篇关于NotePad无法使用PrintStream输出到文本文件来识别java代码中的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆