JAVA產生字母和隨機數字並組建檔案,java產生字母
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); }}