gpt4 book ai didi

java - 为什么 printwriter 实际上不写入文件?

转载 作者:行者123 更新时间:2023-12-04 20:46:10 24 4
gpt4 key购买 nike

我已经为这个问题苦苦挣扎了几个小时,我不确定如何才能最好地继续下去。

代码应该读取一个文件并使用凯撒密码对其进行编码,将其写入磁盘到一个新文件中并附加 _encoded。

它确实创建了文件,但每次都是空白的。当我查看文档时,它说要确保冲洗并关闭打印器。我已经做到了。

我还认为这可能是因为我以迂回的方式解决问题;我想知道是不是使用了FileOutputstream。

        Scanner sc=new Scanner(inFile);
//File outFile=new File("caesar_encoded.txt");

//FileOutputStream outFileStream=new FileOutputStream(outFile);
PrintWriter outStream=new PrintWriter("caesar_encoded.txt");

while(sc.hasNext())
{
String phrase = sc.nextLine().toUpperCase();
for (int i = 0; i < phrase.length(); i++) {
if (Character.isLetter(phrase.charAt(i))) {
for (int j = 0; j < alpha.length; j++) {
if (phrase.charAt(i) == alpha[j]) {
if (j == alpha.length - 1) {
outStream.print(alpha[0]);
} else {
outStream.print(alpha[j + 1]);
}
}
}
} else {
outStream.print(phrase.charAt(i));
}
}
outStream.println();
}
outStream.flush();
outStream.close();

sc.close();

我尝试这样做,但结果相同:

Scanner sc=new Scanner(inFile);
File outFile=new File("caesar_encoded.txt");
FileOutputStream outFileStream=new FileOutputStream(outFile);
PrintWriter outStream=new PrintWriter(outFileStream);

最佳答案

装饰它:

PrintWriter pw = new PrintWriter(new FileWriter("caesar_encoded.txt"), true);

或者这个:

PrintStream ps = new PrintStream(new FileOutputStream("caesar_encoded.txt"), true);

你当然明白Writer(字符流)和OutputStream(字节流)层次结构的区别。你通常不会跨越两者。您可以使用 OutputStreamWriterOutputStream 跳转到 Writer,但您不能走另一条路。

这段代码对我来说运行完美。我相信你的方法,猜测 SHIFTED 数组。在任何情况下,我都会在 encrypted.txt 文件中获得输出。我认为问题出在您的扫描仪上。

package io;

import java.io.*;

/**
* PrintWriterDemo
* @author Michael
* @link http://stackoverflow.com/questions/12849855/why-doesnt-printwriter-actually-write-to-the-file/12849867#comment17389040_12849867
* @link http://en.wikipedia.org/wiki/Caesar_cipher
* @since 10/11/12 7:17 PM
*/
public class PrintWriterDemo {

public static void main(String[] args) {
try {
File unencrypted = new File((args.length > 0) ? args[0] : "resources/unencrypted.txt");
File encrypted = new File((args.length > 1) ? args[1] : "resources/encrypted.txt");
caesar(unencrypted, encrypted);
} catch (Exception e) {
e.printStackTrace();
}
}

private static final char [] SHIFTED = {
'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C' };

public static void caesar(File unencrypted, File encrypted) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(unencrypted));
PrintWriter pw = new PrintWriter(encrypted);
String line = "";
while ((line = br.readLine()) != null) {
String phrase =line.toUpperCase();
for (int i = 0; i < phrase.length(); i++) {
if (Character.isLetter(phrase.charAt(i))) {
for (int j = 0; j < SHIFTED.length; j++) {
if (phrase.charAt(i) == SHIFTED[j]) {
if (j == SHIFTED.length-1) {
pw.print(SHIFTED[0]);
} else {
pw.print(SHIFTED[j+1]);
}
}
}
} else {
pw.print(phrase.charAt(i));
}
}
pw.println();
}
pw.flush();
pw.close();
br.close();
}
}

这是我的 unencrypted.txt 输入:

This should work out fine.
I have no idea what the problem is with the original code.
But I do know that this writes just fine.

这是我的 encrypted.txt 输出:

UIJT TIPVME XPSL PVU GJOF.
J IBWF OP JEFB XIBU UIF QSPCMFN JT XJUI UIF PSJHJOBM DPEF.
CVU J EP LOPX UIBU UIJT XSJUFT KVTU GJOF.

关于java - 为什么 printwriter 实际上不写入文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12849855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com