java实现只接收数字输入,输入exit退出程序,并打印输入的数字,同时写入文件
package fileAdnIo_exercise;import java.io.BufferedOutputStream;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.util.ArrayList;import java.util.List;import java.util.Scanner;import java.util.Collections;/** * @author 苏宝伢 E-mail:by.su@qq.com * @version 创建时间: 2017年3月31日 下午5:41:33 */public class AceptInputInt { public static void main(String[] args) { Scanner scan = new Scanner(System.in); ListintSet = new ArrayList (); for(int i=0;i<1000;i++){ String acept = scan.nextLine(); try{ if(acept.equals("exit")){ break; }else if(acept.matches("\\D*") || acept.equals("")){ System.out.println("请重新输入数字。谢谢~!"); continue; }else{ intSet.add(Integer.valueOf(acept)); } }catch(Exception e){ System.out.println(intSet); e.printStackTrace(); } } System.out.println("输入的数字为:" + intSet); System.out.println("--------------------------------------"); OutputStreamWriter fout = null; try { fout = new OutputStreamWriter(new FileOutputStream("D:\\bysu\\interface\\java8\\src\\fileAndIO\\FileInputStreamDemoCopy.java"),"UTF-8"); for(int i:intSet){ try { fout.write(i + ""); } catch (IOException e) { e.printStackTrace(); } } } catch (IOException e) { e.printStackTrace(); }finally{ if(fout != null){ try { fout.close(); } catch (IOException e) { e.printStackTrace(); } } System.out.println("文件写入完毕~!"); } System.exit(0); }}