- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请看一下我的代码。
import java.util.*;
import java.io.*;
public class LibraryDriver {
public static void main(String[] theArgs) {
String theAuthor = "";
String theTitle = "";
Scanner input = null;
PrintStream output = null;
try {
input = new Scanner(new File("LibraryIn1.txt"));
output = new PrintStream(new File("LibraryOut.txt"));
} catch (Exception e) {
System.out.println("Difficulties opening the file! " + e);
System.exit(1);
}
ArrayList < String > authors = new ArrayList < String > ();
ArrayList < Book > books = new ArrayList < Book > ();
while (input.hasNext()) {
// Read title
theTitle = input.nextLine();
// Read author(s)
theAuthor = input.nextLine();
authors = new ArrayList < String > (getAuthors(theAuthor));
// Insert title & author(s)into a book
// Add this book to the ArrayList<Book> of books
books.add(new Book(theTitle, authors));
authors.clear();
}
// Instantiate a Library object filled with the books read thus far
// and write the contents of the library to the output file
Library lib = new Library(books);
output.println("PRINTS INITIAL BOOK LIST:");
output.println(lib);
// Sort the current contents of the library
lib.sort();
// and write the contents of the sorted library to the output file
output.println("\nPRINTS SORTED BOOK LIST:");
output.println(lib);
// Close the first input file and open the second input file.
// Read the titles and authors from the second input file,
// add them to the library, and write the contents of the
// library to the output file.
input.close();
try {
input = new Scanner(new File("LibraryIn2.txt"));
output = new PrintStream(new File("LibraryOut.txt"));
} catch (Exception e) {
System.out.println("Difficulties opening the file! " + e);
System.exit(1);
}
while (input.hasNext()) {
theTitle = input.nextLine();
theAuthor = input.nextLine();
authors = (getAuthors(theAuthor));
Book b = new Book(theTitle, authors);
lib.add(b);
}
output.println("\nPRINT WITH NEW BOOK UNSORTED:");
output.println(lib);
// Sort the library and write it to the output file
lib.sort();
output.println("\nPRINT ALL SORTED BOOK LIST:");
output.println(lib);
// The following tests the findTitles method, i.e. test
// the findTitles method by passing “Acer Dumpling” and
// then “The Bluff”:
// Write only the "Acer Dumpling" books to the output file
output.println("\nPRINT ALL ACER DUMPLINGS:");
for (Book b: lib.findTitles("Acer Dumpling")) {
output.println(b);
}
// Write only the "The Bluff" books to the output file
output.println("\nPRINT ALL THE BLUFFS:");
for (Book b: lib.findTitles("The Bluff")) {
output.println(b);
}
// Close all open files and end main.
input.close();
output.close();
}
// Header for method that separates author names and
// returns an ArrayList<String> containing the author names
public static ArrayList < String > getAuthors(String theAuthors) {
String[] temp = theAuthors.split("\\*");
ArrayList < String > result = new ArrayList < String > (Arrays.asList(temp));
return result;
}
}
运行该程序后,输出文件仅加载如下:
PRINT WITH NEW BOOK UNSORTED:
(the list of books' title and authors)
PRINT ALL SORTED BOOK LIST:
(the list of books' title and authors)
PRINT ALL ACER DUMPLINGS:
(the list of title with acer dumpling)
PRINT ALL THE BLUFFS:
(the list of title with the bluff)
前两部分“打印初始图书列表”和“打印排序图书列表”丢失,但我不知道如何解决。谢谢!
最佳答案
您问过,但不知道如何解决。
我的回答如下:
您还可以引用以下链接:
https://www.eclipse.org/community/eclipse_newsletter/2017/june/article1.php
关于java - 仅使用 PrintStream 输出部分结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58705333/
PrintStream的printf方法的返回类型是PrintStream,每次调用它都会返回这个对象(其他打印方法都是void)。那么为什么要这样设计呢?就此而言,如果您有一个任何类型的对象,并且该
我正在运行一个由另一个人编写的 Java 程序,其数据量超过了该程序最初设计的数据量,例如输入文件长 10 倍,大致为二次运行时间。我遇到了不同的问题,现在的目标是一点一点地解决它们。 在执行期间,当
我在本地主机的端口打开一个服务器套接字,例如11111,并且我还包括流数据的打印流,我希望像这样整齐地输出它 PrintStream ps=new PrintStream(socket.getOutp
我使用的是 eclipse IDE,有时,根据代码的不同,System.err 输出会先于 System.out 打印。例如: public static void main(String[]
import java.io.PrintStream; import java.util.Scanner; public class OutputTest { public static vo
in = new BufferedReader (new InputStreamReader(client.getInputStream())); out = new DataOutputStream
当我使用 System.out.println 打印日志消息时,我正在编写一个基本的服务器程序。我编写了一个基本的类文件,使用它来写入日志。如果我写以下内容: System.out.println("
这个问题在这里已经有了答案: File Write - PrintStream append (2 个答案) 关闭 6 年前。 我想使用 PrintStream 将测试结果写入已创建的文本文件。可惜
我正在查看一个类,该类通过其构造函数获取一个 OutputStream 对象,并使用它创建一个 PrintStream 对象,如下所示: this.pout = new PrintStream(out
我不明白为什么我的代码导致 PrintStream 转到新行: // displays the total time of the leak in months from the calculateL
我正在制作一个服务器客户端系统,其中客户端将向服务器写入一条消息,服务器将其保存为字符串并将其打印在控制台中。但是,当系统尝试读取该行时,我会在控制台中收到“PrintStream 错误”。没有错误或
我在第 58 行收到错误,表示未找到该文件,但 PrintStream 不应该创建该文件吗? (注意:根本没有创建任何文件。) 谢谢! 为什么需要添加更多详细信息我已经说了什么我需要说 import
程序按预期在我的控制台上输出,但它现在只能创建空白输出文件。我不知道出了什么问题。这是我认为是我的麻烦根源的方法: public static PrintStream getOutputPri
我正在运行一些不安全的代码,我已将其 stdout 和 stderr 流设置为包装在 PrintStream< 中的 FileStream/s。 (标准输出/错误必须重定向。) 是否有任何方法可以配置
请看一下我的代码。 import java.util.*; import java.io.*; public class LibraryDriver { public static void
我有一个用 Java 编写的 GUI 程序,它使用 System.out.println 将数据输出到命令行。数据旨在通过管道输送到另一个程序中。作为示例,我将通过 head 传输程序: $ java
这个问题已经有答案了: System.out to a file in java (7 个回答) Redirect System.out.println (6 个回答) 已关闭 3 年前。 怎么会Sy
Java 文档 page System 类表示其字段之一称为“out”: 静态打印流输出 如果我然后查看文档 page对于 PrintStream 类,它表示其继承字段之一称为“out”。如果我然后单
我想使用 PrintWriter 或 PrintStream 将格式化字符串写入输出流(我无法控制其创建)。然而,刷新底层 OutputStream 会对性能造成很大影响。 是否需要刷新 PrintW
最新 SSCCEE 为什么下面的示例输出不同的字符串? package tests.java; import java.io.IOException; import java.io.OutputStr
我是一名优秀的程序员,十分优秀!