java 将 OutputStream 写入文件的代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15935219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:20:17  来源:igfitidea点击:

Code to write OutputStream into a file

javafile-ioio

提问by user2254173

I need to write some output into a file using Java. Currently my code is writing the OutputStream on system console. My concern is I have to write process.getOutputStream()output which returns an OutputStream instance.

我需要使用 Java 将一些输出写入文件。目前我的代码正在系统控制台上编写 OutputStream。我担心的是我必须编写process.getOutputStream()返回一个 OutputStream 实例的输出。

final PrintWriter writer = new PrintWriter(process.getOutputStream());
writer.println(200);
writer.close();

回答by óscar López

Try this:

试试这个:

PrintWriter writer = new PrintWriter(new File("path/to/output-file.txt"));

It's just a matter of switching the output of the PrintWriterto the target you wish to use, a file in this case.

只需将 的输出切换PrintWriter到您希望使用的目标,在本例中为文件。

回答by VKPRO

   BufferedWriter write = new BufferedWriter( new FileWriter( "output.txt"));
   write.write("your data");

回答by user207421

Your question makes no sense. A ProcessOutputStreamisn't a file. It is a stream that is read by the process. What the process does with the data you send it is its business, not yours.

你的问题没有意义。AProcessOutputStream不是文件。它是一个被进程读取的流。该流程如何处理您发送的数据是它的业务,而不是您的业务。