将Java文档的输出写入.rtf文件的最简单方法是什么?

时间:2018-01-16 15:49:34

标签: java output rtf

我想知道将输出(下面的非注释代码的最后一行)写入.rtf文件的最简单方法是什么,这样我就可以用斜体格式化某些方面,以及保持连续,复制和粘贴,我所有的引用列表。有没有办法做我想要的那么简单?我是Java的初学者,并且不想要任何复杂的事情来处理。

/* (c) Tyler Holley January, 2018
 * CitationGenerator Version 0.2
 * 
 * User inputs academic source information and gets a proper citation back ready for copy and pasting into a Works Cited page.
 */

import java.util.Scanner;

class CitationGenerator {
    public static String bookFormat(String author, String title, String publisher, int pubDate) {
        // 
        String bFormat = author + ". " + title + ", " + publisher + ", " + pubDate + ".";

        return bFormat;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        String error1 = "ERROR: INVALID INPUT."; // Catchall error
        String errorN = "ERROR: No other formats are currently supported in this version."; // Filler error while WIP

        System.out.println("Welcome to CitationGenerator v0.1!");
        System.out.print("What format is your citation in? MLA/APA/Chicago: "); // Add APA/Chicago support
        String format = in.next();

        if (format.equalsIgnoreCase("MLA")) { // equalsIgnoreCase ignores case inputted so MLA, mLa, mla, etc. are all valid
            System.out.print("\nIs your source a book, website, or other? ");
            String sourceType = in.next();
            // Try and figure out how to clear the console after the user inputs sourceType 

            if (sourceType.equalsIgnoreCase("book")) {
                System.out.print("\nAuthor's First Name: ");
                String fName = in.next();

                System.out.print("Author's Last Name: ");
                String lName = in.next();
                in.nextLine();

                System.out.print("\nBook Title: ");
                String title = in.nextLine();

                System.out.print("\nPublisher Name: ");
                String publisher = in.nextLine();

                System.out.print("\nPublication Date: ");
                int pubDate = in.nextInt();

                String author = lName + ", " + fName; // Converts fName and lName to one concatonated String

                System.out.println("\n\nHere is your citation! Don't forget to italicize the title!\n");
                // Try to automatically italicize text when converting program to a .rtf
                System.out.println(bookFormat(author, title, publisher, pubDate));


                // GOAL: Alternate to the println below :
                //System.out.println("\n\nYour citation is ready, would you like to save it to a/the .rtf document? y/n");
                // This would branch into an if/else statement to print either to a document or continue to terminal output.
            }
      }
}

1 个答案:

答案 0 :(得分:-1)

与写入任何文件非常相似:

DataOutputStream dos;
File file = new File("\\your\\file\\path\\file.rtf");
try {
    dos = new DataOutputStream(new FileOutputStream(file));
    dos.writeBytes(bookFormat(author, title, publisher, pubDate));
    dos.close();
catch( //IOexception or similar etc

<强>输出:

  

姓,名字。 booktitle,bookpublisher,1234。