如何将Java程序的输出结果直接写入文件的操作步骤详细说明?
- 内容介绍
- 文章标签
- 相关推荐
本文共计548个文字,预计阅读时间需要3分钟。
1. 将输出结果保存到txt文件,直接安排代码:java// 导包import java.io.FileNotFoundException;import java.io.PrintStream;
public class Test { public static void main(String[] args) throws FileNotFoundException { // 创建一个打印流,指向当前目录下的result.txt文件 PrintStream out=new PrintStream(result.txt);
// 将标准输出流重定向到打印流 System.setOut(out);
// 输出信息到文件 System.out.println(Hello, World!); }}
1.将输出结果输出到txt文件
直接安排代码:
//导包 import java.io.FileNotFoundException; import java.io.PrintStream; public class Test { public static void main(String[] args) throws FileNotFoundException { // 创建一个打印输出流,输出的目标是D盘下的1.txt文件 PrintStream ps = new PrintStream("D:\\1.txt"); //可能会出现异常,直接throws就行了 System.setOut(ps); //把创建的打印输出流赋给系统。
本文共计548个文字,预计阅读时间需要3分钟。
1. 将输出结果保存到txt文件,直接安排代码:java// 导包import java.io.FileNotFoundException;import java.io.PrintStream;
public class Test { public static void main(String[] args) throws FileNotFoundException { // 创建一个打印流,指向当前目录下的result.txt文件 PrintStream out=new PrintStream(result.txt);
// 将标准输出流重定向到打印流 System.setOut(out);
// 输出信息到文件 System.out.println(Hello, World!); }}
1.将输出结果输出到txt文件
直接安排代码:
//导包 import java.io.FileNotFoundException; import java.io.PrintStream; public class Test { public static void main(String[] args) throws FileNotFoundException { // 创建一个打印输出流,输出的目标是D盘下的1.txt文件 PrintStream ps = new PrintStream("D:\\1.txt"); //可能会出现异常,直接throws就行了 System.setOut(ps); //把创建的打印输出流赋给系统。

