码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA生成字母和随机数字并生成文件

时间:2015-11-23 08:49:17      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

package com.ishow.control.code;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Random;

/**
 * @author Lee
 * @version 创建时间:Oct 9, 2015 4:12:25 PM
 */        
public class CreateCodeController{
    /**
     * 生成兑换码
     * @return
     * @throws IOException 
     */
    public static void main(String[] args){
        
        Long start = System.currentTimeMillis();
        
        String prefix = "LF";    //前缀
         int num = 10;//数字位数
         int count = 10000;//生成数量

         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH时mm分");
        
        //生成文件地址
         File f = new File("C:\\Documents and Settings\\Administrator\\桌面\\生成码" + formatter.format(System.currentTimeMillis()) + ".txt");
        
        OutputStreamWriter writer = null;
        BufferedWriter bw = null;
        
        Random random = new Random();
        try {
            OutputStream os = new FileOutputStream(f);
            writer = new OutputStreamWriter(os);
            bw = new BufferedWriter(writer);
            int i=0;
            while(i<count){
                String str = "";
                for (int j = 0; j < num; j++) {
                    int number = random.nextInt(10);      
                    str+=number+"";
                }
                str = prefix+str;
                try {
                    bw.write(str+"\r\n");
                } catch (Exception e) {
                    i--;
                }
              i++;
            }
            bw.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Long end = System.currentTimeMillis();
        System.out.println("bufferedWrite And FileWriterTest‘s time---------" + (start - end)/1000d);
    }
}

  

JAVA生成字母和随机数字并生成文件

标签:

原文地址:http://www.cnblogs.com/shouce/p/4987350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!