5 回答
TA贡献1982条经验 获得超2个赞
由于您已经在使用PrintWriter用于写入文件,因此将通用代码移至辅助方法:
private static void printToFile(char space, char hash, int spaceCount, int hashCount){
try (PrintWriter outFile = new PrintWriter(new FileWriter("output.txt"))) {
printToWriter(outFile, space, hash, spaceCount, hashCount);
} catch(FileNotFoundException e) {
e.getCause();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void printToScreen(char space, char hash, int spaceCount, int hashCount){
PrintWriter out = new PrintWriter(System.out);
printToWriter(out, space, hash, spaceCount, hashCount);
out.flush(); // Do not close
}
private static void printToWriter(PrintWriter out, char space, char hash, int spaceCount, int hashCount){
for (int i = 0; i < 8; i++) {
out.print(marioStepCreator(space, spaceCount));
out.println(marioStepCreator(hash, hashCount));
hashCount++;
spaceCount--;
}
}
当然,您真的需要修复printToFile方法中的异常处理。
由于您使用 try-with-resources 立即关闭文件编写器,因此无需刷新输出。然而,包装必须被刷新,以防它正在缓冲,但不应该被关闭,PrintWriter因为System.out我们不想关闭System.out。
TA贡献1772条经验 获得超8个赞
您可以隔离 for 循环以生成一个 String 消息,该消息可以使用 writer 或 System.out.println 在一行中打印;
private static void printToFile(char space, char hash, int spaceCount, int hashCount){
try (PrintWriter outFile = new PrintWriter(new FileWriter("output.txt"))) {
outFile.write(printMessage(space, hash, spaceCount, hashCount));
outFile.flush();
} catch(FileNotFoundException e) {
e.getCause();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void printToScreen(char space, char hash, int spaceCount, int hashCount){
System.out.print(printMessage(space, hash, spaceCount, hashCount));
}
private static String printMessage(char space, char hash, int spaceCount, int hashCount) {
String message = "";
for (int i = 0; i < 8; i++) {
message += marioStepCreator(space, spaceCount) + marioStepCreator(hash, hashCount) + "\n";
hashCount++;
spaceCount--;
}
return message;
}
TA贡献1906条经验 获得超3个赞
你也可以尝试做这样的事情 -
private static List<String> getOutput(char space, char hash, int spaceCount, int hashCount) {
List<String> output = new ArrayList<>();
for (int i = 0; i < 8; i++) {
output.add(String.valueOf(marioStepCreator(space, spaceCount)));
output.add(String.valueOf(marioStepCreator(hash, hashCount)));
hashCount++;
spaceCount--;
}
return output;
}
private static void printToFileAndScreen(char space, char hash, int spaceCount, int hashCount) {
try (PrintWriter outFile = new PrintWriter(new FileWriter("output.txt"))) {
getOutput(space, hash, spaceCount, hashCount).stream().forEach(s ->
{
outFile.write(s);
outFile.flush();
});
} catch (FileNotFoundException e) {
e.getCause();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void printToScreen(char space, char hash, int spaceCount, int hashCount) {
getOutput(space, hash, spaceCount, hashCount).stream().forEach(s -> System.out.println(s));
}
通过这种方式,您可以将主要业务逻辑与输出消费者分开。此外,您可以为不同的编写器实现和连接具有“write()”方法的接口。这种方式很容易编写测试用例。
TA贡献1815条经验 获得超13个赞
这个怎么样:
private static void printToFileAndScreen(char space, char hash, int spaceCount, int hashCount){
try (PrintWriter outFile = new PrintWriter(new FileWriter("output.txt"))) {
for (int i = 0; i < 8; i++) {
String spaceString = String.valueOf(marioStepCreator(space, spaceCount));
String hashString = String.valueOf(marioStepCreator(hash, hashCount));
outFile.write(spaceString);
outFile.write(hashString);
outFile.write("\n");
outFile.flush();
System.out.print(spaceString);
System.out.print(hashString);
System.out.println();
hashCount++;
spaceCount--;
}
} catch(FileNotFoundException e) {
e.getCause();
} catch (IOException e) {
e.printStackTrace();
}
}
TA贡献1790条经验 获得超9个赞
这个怎么样:
private static void printToFile(char space, char hash, int spaceCount, int hashCount) {
try (PrintWriter outFile = new PrintWriter(new FileWriter("output.txt"))) {
loop(spaceCount, hashCount, (sCount, hCount) -> {
outFile.write(String.valueOf(marioStepCreator(space, sCount)));
outFile.flush();
outFile.write(String.valueOf(marioStepCreator(hash, hCount)));
outFile.flush();
outFile.write("\n");
});
} catch (FileNotFoundException e) {
e.getCause();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void printToScreen(char space, char hash, int spaceCount, int hashCount) {
loop(spaceCount, hashCount, (sCount, hCount) -> {
System.out.print(marioStepCreator(space, sCount));
System.out.print(marioStepCreator(hash, hashCount));
System.out.println();
});
}
private static void loop(int spaceCount, int hashCount, final BiConsumer<Integer, Integer> consumer) {
for (int i = 0; i < 8; i++) {
consumer.accept(spaceCount, hashCount);
hashCount++;
spaceCount--;
}
}
添加回答
举报