- public class Test1 {
- public static void main(String[] args) throws IOException {
- // BufferedOutputStream bos = new BufferedOutputStream(new
- // FileOutputStream("D:\\xx.txt"));
- File file = new File("D:/xx.txt");
- BufferedWriter bw = new BufferedWriter(new FileWriter(file));
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入一个整数");
- TreeSet<Integer> ts = new TreeSet<Integer>(new Comparator<Integer>() {
- @Override
- public int compare(Integer i1, Integer i2) {
- int num = i2.compareTo(i1);
- return num == 0 ? 1 : num;
- }
- });
- while (ts.size() < 5) {
- try {
- String line = sc.nextLine();
- Integer i = Integer.parseInt(line);
- ts.add(i);
- } catch (Exception e) {
- System.out.println("类型输入错误,请重新输入");
- }
- }
- bw.write(ts.toString());
- bw.close();
- BufferedReader br=new BufferedReader(new FileReader(file));
- String line=br.readLine();
- StringBuffer sb=new StringBuffer(line.substring(1, line.length()-1));
- sb=sb.reverse().insert(0, "[").append("]");
- System.out.println(sb);
- br.close();
-
- }
- }
复制代码
|