Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package gen_csv;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.Random;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- *
- * @author Кристина
- */
- public class Main {
- /**
- * @param args the command line arguments
- */
- static Random random = new Random(new Date().getTime());
- public static void main(String[] args) {
- // TODO code application logic here
- String args0 = "-col";
- String args1 = "500";
- String args2 = "-row";
- String args3 = "9871";
- String args4 = "-len";
- String args5 = "30";
- String args6 = "-out";
- String args7 = "out.csv";
- try {
- checkArgs(args0, args1, args2, args3, args4, args5, args6, args7);
- genRandomFile(args1, args3, args5, args7);
- }
- catch(IllegalArgumentException event) {
- System.err.println(event.getMessage());
- }
- }
- public static void checkArgs(String col, String numberCol, String row, String numberRow, String len, String numberLen, String out, String outfileName){
- Pattern integerPat = Pattern.compile("^[1-9](\\d+)?");
- Pattern filenamePat = Pattern.compile("[\\w\\d]+\\.csv");
- int testLong;
- if ((col.equals("-col"))&&(row.equals("-row"))&&(len.equals("-len"))&&(out.equals("-out"))){
- Matcher m = filenamePat.matcher(outfileName);
- boolean b = m.matches();
- if (b==false) fail("gen_csv: неправильно заданно имя выходящего файла!");
- //File outFile = new File(outfileName);
- // if (outFile.exists()) fail("gen_csv: заданный файл уже существует в этой дериктории: " + outfileName);
- m = integerPat.matcher(numberCol);
- b = m.matches();
- if (b==false) fail("gen_csv: некоректно задано значение аргумента col");
- testLong = Integer.parseInt(numberCol);
- if (!(testLong <= 1000)) fail("gen_csv: максимальное количество столбцов не должно превышать 1000");
- m = integerPat.matcher(numberRow);
- b = m.matches();
- if (b==false) fail("gen_csv: некоректно задано значение аргумента row");
- testLong = Integer.parseInt(numberRow);
- if (!(testLong <= 1000000)) fail("gen_csv: максимальное количество строк не должно превышать 1000000");
- m = integerPat.matcher(numberLen);
- b = m.matches();
- if (b==false) fail("gen_csv: некоректно задано значение аргумента len");
- testLong = Integer.parseInt(numberLen);
- if (!(testLong <= 300)) fail("gen_csv: максимальная длина строки не должна превышать 300");
- }
- else fail("gen_csv: некоректно заданы названия аргументов!");
- }
- public static void genRandomFile(String numberCol, String numberRow, String numberLen, String outfileName){
- File outFile = new File(outfileName);
- try {
- PrintWriter out = new PrintWriter(new FileWriter(outFile));
- ArrayList<String> types = new ArrayList<String>();
- for (int i = 0; i < Integer.parseInt(numberCol); i++){
- types.add(i, genRandomType());
- if (i < Integer.parseInt(numberCol)-1) out.print(genRandomString("20") + " " + types.get(i) + "; ");
- else out.print(genRandomString("20") + " " + types.get(i));
- }
- /* for (int i = 0; i < Integer.parseInt(numberRow); i++){
- out.println("");
- for (int j = 0; j < Integer.parseInt(numberCol); j++){
- if (types.get(j).equals("Integer")) out.print(genRandomInt() + ";");
- if(types.get(j).equals("Float")) out.print(genRandomFloat() + ";");
- if(types.get(j).equals("String")) out.print(genRandomString(numberLen) + ";");
- if(types.get(j).equals("Date")) out.print(genRandomDate() + ";");
- }
- }*/
- out.close();
- }
- catch(IOException e){
- System.err.println(e.getMessage());
- }
- }
- public static int genRandomInt(){
- int randomInt = random.nextInt();
- return randomInt;
- }
- public static float genRandomFloat(){
- float randomFloat = random.nextFloat()* random.nextInt(500000)-random.nextInt(500000);
- return randomFloat;
- }
- public static String genRandomString(String longStr){
- String str="qwertyuiopasdfghjklzxcvbnm";
- String strOut = "";
- int longString = random.nextInt(Integer.parseInt(longStr));
- int j;
- for(int i = 0; i <= longString; i++){
- j = random.nextInt(str.length());
- strOut += str.charAt(j);
- }
- return strOut;
- }
- public static String genRandomDate(){
- String str = "";
- int year = 1900 +random.nextInt(111);
- int month = 1 + random.nextInt(12);
- int day = 1;
- if ((month == 1)|(month == 3)|(month == 5)|(month == 7)|(month == 8)|(month == 10)|(month == 12)){
- day = random.nextInt(31);
- }
- if ((month == 4)|(month == 6)|(month == 9)|(month == 11)){
- day = random.nextInt(30);
- }
- if (month == 2){
- if (year%4==0) day = random.nextInt(29);
- else day = random.nextInt(28);
- }
- if (month <= 9) str += day + ".0" + month + "." + year;
- else str +=day + "." + month + "." + year;
- return str;
- }
- public static String genRandomType(){
- String str = "";
- int randType = random.nextInt(4);
- if (randType == 0) str += "Float";
- if (randType == 1) str += "String";
- if (randType == 2) str += "Integer";
- if (randType == 3) str += "Date";
- return str;
- }
- protected static void fail(String msg) throws IllegalArgumentException {
- throw new IllegalArgumentException(msg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement