Java简单从文件读取和输出
用Scanner输入,用PrintStream输出
功能:从in.txt读入,输出到out.txt
代码:
package ioTest; import java.io.*;
import java.util.Scanner; public class TestMain { public static void main(String[] args) {
try {
Scanner sc=new Scanner(new File("in.txt"));
PrintStream ps=new PrintStream("out.txt");
while(sc.hasNext()){
int a=sc.nextInt();
System.out.print(a+" ");
ps.println(a);
} } catch (Exception e) {
e.printStackTrace();
}
} }
和下面的一样
结果: