zoukankan      html  css  js  c++  java
  • Java 产生随机数并写入txt文档中

    源代码:

    import java.io.*;
    import java.util.Random;
    
    
    public class AlgorithmTest 
    {    
        public static void main(String[] args)
        {
            String filepath = System.getProperty("user.dir");    
            filepath +="\data.txt";
            System.out.println(filepath);
            
            try 
            {
                File file = new File(filepath);            
                if(!file.exists())
                {    //如果不存在data.txt文件则创建
                    file.createNewFile();
                    System.out.println("data.txt创建完成");                
                }
                FileWriter fw = new FileWriter(file);        //创建文件写入
                BufferedWriter bw = new BufferedWriter(fw);
                
                //产生随机数据,写入文件
                Random random = new Random();
                for(int i=0;i<10000;i++)
                {    
                    int randint =(int)Math.floor((random.nextDouble()*100000.0));    //产生0-10000之间随机数        
                    bw.write(String.valueOf(randint));        //写入一个随机数
                    bw.newLine();        //新的一行
                }
                bw.close();
                fw.close();
                
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }        
        }    
    }
  • 相关阅读:
    Jenkins安装后,安装插件失败。报错SunCertPathBuilderException
    计算机网络
    abaqus
    品优购
    html5 css3
    css定位
    元素的显示与隐藏 / 精灵图
    学成在线案例
    css(3)
    css(2)
  • 原文地址:https://www.cnblogs.com/sengzhao666/p/11011013.html
Copyright © 2011-2022 走看看