java编写一个含有100个随机值的文本,且从文本中读回数据并以升序显示数据

发布时间:2020-07-19 06:41:20 作者:梦T醒
来源:网络 阅读:339
import java.io.*;
import java.util.*;
public class WriteArray
{
    public static void main(String[] args) throws Exception
    {
        System.out.println("请输入文件名");
        Scanner fileName=new Scanner(System.in);
        File file=new File(fileName.nextLine());

        if(!file.exists())//判断文件是否存在
        {
            PrintWriter output=new PrintWriter(file);
            Random r=new Random();
            for(int i=0;i<100;i++)
            {
                output.print(r.nextInt(1000)+" ");
            }
            output.close();
        }
        else
        {
            Scanner input=new Scanner(file);
            int[] arr =new int[100];
            int i=0;
            while (input.hasNext())//如果还有数据读取,返回true
            {
                arr[i++]=input.nextInt();
            }
            input.close();
            Arrays.sort(arr);//升序排列
            for(i=0;i<arr.length;i++)
            {
                if(i%10==9)//换行输出
                {
                    System.out.println(arr[i]);
                }
                else
                {
                    System.out.print(arr[i]+"\t");
                }
            }
        }
    }
}
推荐阅读:
  1. 从文本文件导入数据库
  2. Js数组随机显示文字,文本随机出现

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

文件升序排列 ava 回数

上一篇:Spring-batch (ItemProcessor) 数据处理过程

下一篇:Fitnesse使用系列二

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》