import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class Prcatice6 {
/**
* 从键盘接收多个int型的整数,直到用户输入end结束
要求:把所有的整数按倒序写到D:\\number.txt中
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//创建键盘录入对象
Scanner sc = new Scanner(System.in);
List<String> list = new ArrayList<>();
FileOutputStream fos = new FileOutputStream("number.txt");
System.out.println("请输入一个整数,按end结束:");
while(true){
String line = sc.nextLine();
if(line.equals("end")){
break;
}
list.add(line);
//fos.write(line.getBytes());
//System.out.println(line.getBytes());
//fos.write((byte)Integer.parseInt(line));
//fos.write("\r\n".getBytes());
}
Collections.reverse(list);
Iterator<String> it = list.iterator();
while(it.hasNext()){
fos.write(it.next().getBytes());
fos.write("\r\n".getBytes());
}
/*String [] arr = (String[]) list.toArray();
for(int i=arr.length-1;i>=0;i++){
int b = Integer.parseInt(arr[i]);
byte c = (byte)b;
fos.write(c);
} */
fos.close();
}
}
|
|