-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomStringToFile.java
More file actions
96 lines (85 loc) · 2.44 KB
/
RandomStringToFile.java
File metadata and controls
96 lines (85 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.multithread;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RandomStringToFile {
// 判断目录路径是否合法
public static boolean TestFilePath(String dirpath) {
boolean result = false;
try {
File dirname = new File(dirpath);
if (dirname.isDirectory()) {
System.out.println("DirPath Already Exists!");
result = true;
} else { // Ŀ¼������
if (dirname.mkdirs()) {
System.out.println("DirPath Create Successfully!");
result = true;
} else {
System.out.println("DirPath Create Fail!");
result = false;
}
}
} catch (Exception e) {
// if any error occurs
e.printStackTrace();
}
return result;
}
// 产生随机字符串
public static String getRandomString(int length) {
String string = "abcdefghijklmnopqrstuvwxyz";
StringBuffer sb = new StringBuffer();
int len = string.length();
for (int i = 0; i < length; i++) {
sb.append(string.charAt((int) Math.round(Math.random() * (len - 1))));
}
return sb.toString();
}
// 写入文件
public static void CreateFile(String dirpath, int filenum) {
String[] num = new String[filenum];
for (int i = 0; i < filenum; i++) {
num[i] = getRandomString(3);
String filepath = dirpath + "\\" + num[i] + ".txt";
File file = new File(filepath);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
// 文件行数
int rownum = (int) (1 + Math.random() * 1000);
for (int j = 0; j < rownum; j++) {
// 每一行随机字符串的长度
int rownum2 = (int) (1 + Math.random() * 100);
String filetext = getRandomString(rownum2);
bw.write(filetext);
bw.newLine();
}
bw.flush();
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
String path = input.next();
int filenum = input.nextInt();
if (TestFilePath(path)) {
CreateFile(path, filenum);
System.out.println("Files Create Successfully!");
}
}
}