Java简单从文件读取和输出
Java简单从文件读取和输出
用Scanner输入,用PrintStream输出
功能:从in.txt读入,输出到out.txt
代码:
1 package ioTest; 2 3 import java.io.*; 4 import java.util.Scanner; 5 6 public class TestMain { 7 8 public static void main(String[] args) { 9 try { 10 Scanner sc=new Scanner(new File("in.txt")); 11 PrintStream ps=new PrintStream("out.txt"); 12 while(sc.hasNext()){ 13 int a=sc.nextInt(); 14 System.out.print(a+" "); 15 ps.println(a); 16 } 17 18 } catch (Exception e) { 19 e.printStackTrace(); 20 } 21 } 22 23 }
结果: