gpt4 book ai didi

java - ProcessBuilder 在 do while 循环后打印空输出

转载 作者:太空宇宙 更新时间:2023-11-04 10:57:06 24 4
gpt4 key购买 nike

我正在使用ProcessBuilder记事本窗口中打开一个文本文件,并根据我的代码打开另一个包含已处理文本的记事本窗口。

What is happening right now is that program opens 1st notepad window with a text I put in it but 2nd windows comes up empty.

有人会建议我需要在哪里进行更改才能在第二个记事本窗口中打印处理后的文本吗? (我将输出包含在代码下方)。我在控制台中打印了正确的结果,但在记事本窗口中没有打印。我以前从未使用过 ProcessBuilder,只能找到如何使用它的简单示例。感谢您的建议。

Scanner in = new Scanner(System.in);
System.out.print("Enter the input file: ");
String inputFileName = in.next();
System.out.print("Enter the output file: ");
String outputFileName = in.next();

String poemFilename = "poem.txt";
String poemoutFilenamo = "newpoem";
int lineNum = 1;

File poemFile = new File(poemFilename);
Scanner fileIn = new Scanner(poemFile);
PrintWriter fileout = new PrintWriter(poemoutFilenamo);

System.out.printf("Read in %s.\nWrote out %s.\n",inputFileName, outputFileName );

ProcessBuilder Poemin = new ProcessBuilder("Notepad.exe", poemFilename);
Poemin.start();

if (fileIn.hasNextLine()) {
do {
String line = fileIn.nextLine();
System.out.printf("/*" + lineNum + "*/ %s\n", line);
lineNum++;
} while (fileIn.hasNextLine());;

ProcessBuilder Poemout = new ProcessBuilder("Notepad.exe", poemoutFilenamo);
Poemout.start();
}

in.close();
fileIn.close();
fileout.close();

应在记事本窗口中打印出以下内容:

/*1*/ Somewhere over the rainbow
/*2*/ Way up high
/*3*/ And the dreams that you dreamed of
/*4*/ Once in a lullaby

最佳答案

你永远不会向fileout写入任何内容。您创建了该文件,但它是空的。

我相信你想要

fileout.printf("/*" + lineNum + "*/ %s\n", line);

您还需要在启动第二个进程之前关闭fileout

关于java - ProcessBuilder 在 do while 循环后打印空输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47256664/

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