Java简单从文件读取和输出

用Scanner输入,用PrintStream输出

功能:从in.txt读入,输出到out.txt

Java简单从文件读取和输出-LMLPHP

代码:

 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();
}
} }

和下面的一样

Java简单从文件读取和输出-LMLPHP

结果:

Java简单从文件读取和输出-LMLPHP

Java简单从文件读取和输出-LMLPHP

Java简单从文件读取和输出-LMLPHP

05-28 14:07