forked from rick2785/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaLesson33.java
More file actions
231 lines (144 loc) · 5.48 KB
/
JavaLesson33.java
File metadata and controls
231 lines (144 loc) · 5.48 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import java.io.*;
// A binary stream is a series of data type values
// To read and write to them you use different methods
// based on the type of data that you are using
public class JavaLesson33{
public static void main(String[] args){
// Create an array of type Customer
Customer[] customers = getCustomers();
// A DataOutputStream allows you to print
// primitive data types to a file
DataOutputStream custOutput = createFile("/Users/rickeyhrabowskie/Documents/workspace/Java Code/src/customers.dat");
// Enhanced for loop for arrays
// Cycles through all of the people in the customers array
for(Customer person : customers){
createCustomers(person, custOutput);
}
// Closes the connection to the DataOutputStream
try {
custOutput.close();
} catch (IOException e) {
System.out.println("An I/O Error Occurred");
// Closes the program
System.exit(0);
}
getFileInfo();
}
// class that defines all the fields for my customers
private static class Customer{
public String custName;
public int custAge;
public double custDebt;
public boolean oweMoney;
public char custSex;
// constructor that's called when a customer is made
public Customer(String custName, int custAge, double custDebt, boolean oweMoney, char custSex){
this.custName = custName; // String
this.custAge = custAge; // Integer
this.custDebt = custDebt; // Double
this.oweMoney = oweMoney; // Boolean
this.custSex = custSex; // Character
}
}
// Creates an array of Customer Objects
private static Customer[] getCustomers(){
Customer[] customers = new Customer[5];
customers[0] = new Customer("John Smith", 21, 12.25, true, 'M');
customers[1] = new Customer("Sally Smith", 30, 2.25, true, 'F');
customers[2] = new Customer("Paul Ryan", 21, 0, false, 'M');
customers[3] = new Customer("Mark Jacobs", 21, 3.25, true, 'M');
customers[4] = new Customer("Steve Nash", 21, 5.25, true, 'M');
return customers;
}
// Create the file and the DataOutputStream that will write to the file
private static DataOutputStream createFile(String fileName){
try{
// Creates a File object that allows you to work with files
// on the hard drive. There is no difference between File
// for character or binary stream writing, or reading
File listOfNames = new File(fileName);
// FileOutputStream is used to write streams of data to a file
// You define whether a new file is created versus appended
// to based on if you add a boolean to the FileOutputStream
// FileOutputStream(file, true) : Appends to the file
// FileOutputStream(file, false) : Creates a new file
// BufferedOutputStream gathers all the data and then writes
// it all at one time (Speeds up the Program)
// DataOutputStream is used to write primitive data to the file
DataOutputStream infoToWrite = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(listOfNames)));
return infoToWrite;
}
// You have to catch this when you call FileWriter
catch(IOException e){
System.out.println("An I/O Error Occurred");
// Closes the program
System.exit(0);
}
return null;
}
// Create a string with the customer info and write it to the file
private static void createCustomers(Customer customer, DataOutputStream custOutput){
try{
// Write primitive data to the file
// Writes a String in UTF format
custOutput.writeUTF(customer.custName);
// Writes an Integer
custOutput.writeInt(customer.custAge);
// Writes a Double
custOutput.writeDouble(customer.custDebt);
// Writes a Boolean
custOutput.writeBoolean(customer.oweMoney);
// Writes a Character
custOutput.writeChar(customer.custSex);
// You also have writeByte, writeFloat, writeLong
// and writeShort
}
catch(IOException e){
System.out.println("An I/O Error Occurred");
System.exit(0);
}
}
// Read info from the file and write it to the screen
private static void getFileInfo(){
System.out.println("Info Written to File\n");
// Open a new connection to the file
File listOfNames = new File("/Users/derekbanas/Documents/workspace3/Java Code/src/customers.dat");
boolean eof = false;
try {
// A DataInputStream object has the methods for reading the data
// The BufferedInputStream gathers the data in blocks
// FileInputStream gets data from the file
DataInputStream getInfo = new DataInputStream(
new BufferedInputStream(
new FileInputStream(listOfNames)));
// Using a while loop that pulls data until EOFException is thrown
while (!eof){
// You have to read data in the exact order it was put in the file
String custName = getInfo.readUTF();
int custAge = getInfo.readInt();
double custDebt = getInfo.readDouble();
boolean oweMoney = getInfo.readBoolean();
char custSex = getInfo.readChar();
System.out.println(custName);
System.out.println(custAge);
System.out.println(custDebt);
System.out.println(oweMoney);
System.out.println(custSex + "\n");
}
} // END OF TRY
catch (EOFException e) {
eof = true;
}
// Can be thrown by FileInputStream
catch (FileNotFoundException e) {
System.out.println("Couldn't Find the File");
System.exit(0);
}
catch(IOException e){
System.out.println("An I/O Error Occurred");
System.exit(0);
}
}
}