-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenfile.java
More file actions
108 lines (96 loc) · 2.83 KB
/
Genfile.java
File metadata and controls
108 lines (96 loc) · 2.83 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
97
98
99
100
101
102
103
104
105
106
107
108
/**
* Source code example for "A Practical Introduction to Data Structures and
* Algorithm Analysis, 3rd Edition (Java)" by Clifford A. Shaffer Copyright
* 2008-2011 by Clifford A. Shaffer
*/
import java.io.*;
import java.util.*;
// -------------------------------------------------------------------------
/**
* Generate a test data file. The size is a multiple of 4096 bytes. Depending on
* the options, you can generate two types of output. With option "-a", the
* output will be set so that when interpreted as ASCII characters, it will look
* like a series of: [space][letter][space][space]. With option "-b", the
* records are short ints, with each record having a value less than 30,000. *
*
* @author Clifford A. Shaffer
* @version 2008-2011
*/
public class Genfile
{
/**
* block size
*/
static final int BLOCK_SIZE = 4096;
/**
* num of records
*/
static final int NUM_REC = 2048; // Because they are short
// ints
/** Initialize the random variable */
static private Random value = new Random(); // Hold the Random class
// object
// ----------------------------------------------------------
/**
* random generator.
*
* @param n
* input
* @return random integer
*/
static int random(int n)
{
return Math.abs(value.nextInt()) % n;
}
// ----------------------------------------------------------
/**
* main method.
*
* @param args
* inputs
* @throws IOException
* exceptions
*/
public static void main(String[] args)
throws IOException
{
short val;
int filesize = Integer.parseInt(args[2]); // Size of file in blocks
DataOutputStream file =
new DataOutputStream(new BufferedOutputStream(new FileOutputStream(
args[1])));
if (args[0].charAt(1) == 'b')
{
// Write out random numbers
for (int i = 0; i < filesize; i++)
{
for (int j = 0; j < NUM_REC; j++)
{
val = (short)(random(29999) + 1);
file.writeShort(val);
}
}
}
else if (args[0].charAt(1) == 'a')
{
// Write out ASCII-readable values
for (int i = 0; i < filesize; i++)
{
for (int j = 0; j < NUM_REC; j++)
{
if ((j % 2) == 1)
{
val = (short)(8224);
}
else
{
val = (short)(random(26) + 0x2041);
}
file.writeShort(val);
}
}
}
file.flush();
file.close();
}
}