-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUdlapSequentialFile.java
401 lines (354 loc) · 9.08 KB
/
UdlapSequentialFile.java
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* by Gerardo Ayala
* UDLAP
* September 2015
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
public class UdlapSequentialFile
{
// representaci�n abstracta del archivo
private File file;
// lector de archivo
private FileReader fileReader;
// Buffer de lectura
private BufferedReader bufferedReader;
// flujo de entrada
private FileInputStream inputStream;
// flujo de salida
private FileOutputStream outputStream;
// partes del nombre del archivo
private String absoluteName;
/** End of File. */
public boolean eof;
/** Number of Lines. */
private int numberOfLines;
private int currentLine;
private char newLine;
private char returnCharacter;
private boolean firstLineHasBeenWritten;
/** Creates the File object from a given physical file location.
* The file must be a simple text file MS-DOS codified.
* @param aPath of the sequential text file location.
* @param aFileName of the sequential text file.
* @param aSuffix of the sequential text file.
*/
public UdlapSequentialFile(String aPath,String aFileName, String aSuffix)
{
absoluteName = aPath + "/" + aFileName + "." + aSuffix;
file = new File(absoluteName);
newLine = '\n';
returnCharacter = '\r';
eof=false;
firstLineHasBeenWritten = false;
currentLine = 0;
}//end Constructor ArchivoSecuencial
/** Opens an existing sequential text file. */
public void open()
{
String line;
//-----------
try
{
inputStream = new FileInputStream(file);
fileReader = new FileReader(absoluteName);
bufferedReader = new BufferedReader(fileReader);
numberOfLines = 0;
while(!eof)
{
line = readALine();
numberOfLines = numberOfLines + 1;
}//end while
try
{
fileReader = new FileReader(absoluteName);
bufferedReader = new BufferedReader(fileReader);
eof=false;
}//end try
catch(FileNotFoundException excepcion)
{
System.out.println(excepcion);
}//end catch
}//end try
catch(FileNotFoundException excepcion)
{
System.out.println("No se encontr� el archivo!!!");
}//end catch
eof=false;
}//end open
/** Constructs a new sequential text file and opens it. */
public void create()
{
numberOfLines = 0;
try
{
file.createNewFile();
outputStream = new FileOutputStream(file);
}//end try
catch(IOException excepcion)
{
System.out.println(excepcion);
}//end catch
}//end create
/** Indicates if End Of File has been reached.
*
* @return true if End Of File has been reached, false otherwise.
*/
public boolean eof()
{
return eof;
}//end eof
/** Provides the number of lines of the sequential text file.
*
* @return an int which value is the number of lines of the sequential text file.
*/
public int getNumberOfLines()
{
return numberOfLines;
}//end getNumberOfLines
private String readALine()
{
String line;
char character;
int integer;
//--------------
// inicia valores de la l�nea a regresar
line = "ni";
character = 'l';
// linea + caracter == "nil" como valor inicial
do
{
try
{
if (character != returnCharacter)
line = line + character;
//end if
character = (char) bufferedReader.read();
integer = (int) character;
if (integer == 65535)
eof = true;
//end if
}//end try
catch(Exception excepcion)
{
eof = true;
}//end catch
}//end do
while ((character!=newLine) && (eof == false));
currentLine = currentLine + 1;
if ((line.length() >= 4))
// no considera la cadena "nil" (posicion de 0 a 2)
line = line.substring(3);
//end if
else
{
line = null;
}//end else
return line;
}//end readALine
/** Reads the next line of the sequential file and interprets it as a String value.
* @return a String object read from the sequential text file.
* In case of reading garbagge it returns null.
* */
public String readString()
{
return readALine();
}//end readString
/** Reads the next line of the sequential file and interprets it as an int value.
* @return an int value read from the sequential text file.
* */
public int readInt()
{
int integer;
String line;
int lineLength;
char lastCharacter;
Integer integerObject;
//-----------
integer = 0;
line = readALine();
if (line != null)
{
lineLength = line.length();
lastCharacter = line.charAt(lineLength-1);
if (lastCharacter == '\r')
line = line.substring(0,lineLength-1);
//endif
System.out.println("zzzzzzzzzzzzzzzzzz: " + line);
integerObject = new Integer(line);
integer = integerObject.intValue();
}//end if
return integer;
}//end readInt
/** Reads the next line of the sequential file and interprets it as a double value.
* @return a double value read from the sequential text file.
* */
public double readDouble()
{
double doubleValue;
String line;
//------------
doubleValue = 0.0;
line = readALine();
if (line != null)
doubleValue = Double.parseDouble(line);
//end if
return doubleValue;
}//end readDouble
/** Reads the next line of the sequential file and interprets it as a char value.
* @return a char value read from the sequential text file.
* */
public char readChar()
{
char character;
String line;
//-------------
character = ' ';
line = readALine();
if (line != null)
character= line.charAt(0);
//end if
return character;
}//end readChar
/** Reads the next line of the sequential file and interprets it as a boolean value.
* @return a boolean value read from the sequential text file.
* */
public boolean readBoolean()
{
boolean booleanValue;
String line;
int lineLength;
char lastCharacter;
Boolean booleanObject;
//-------------
line = readALine();
if (!line.equals("true") && !line.equals("false"))
{
booleanValue=false;
try
{
throw new IOException("----> Can not read a boolean at line " +
currentLine + " of file " +
absoluteName);
}//end try
catch(Exception e)
{
System.out.println(e);
}//end catch
}//end if
else
{
lineLength = line.length();
lastCharacter = line.charAt(lineLength-1);
if (lastCharacter == '\r')
{
line = line.substring(0,lineLength-1);
}//end if
booleanObject = Boolean.valueOf(line);
booleanValue = booleanObject.booleanValue();
}//end else
return booleanValue;
}//end readBoolean
////////////METODOS DE ESCRITURA //////////////////////
private void writeLine(String cadena)
{
// se escribir� un arreglo de BYTES
byte outputArray[];
int i;
//--------------------
// se obtienen los bytes de la cadena (String)
outputArray = cadena.getBytes();
// se escribe el arreglo, byte por byte
i = 0;
while (i < outputArray.length)
{
try
{
// escribimos el i�simo byte
outputStream.write(outputArray[i]);
numberOfLines = numberOfLines + 1;
}//end try
catch(Exception excepcion)
{
System.out.println(excepcion);
}//end catch
i = i + 1;
}//end while
}//end writeLine
/** Writes a String as a new line into the sequential text file.
*
* @param aString which contents will be written.
*/
public void writeString(String aString)
{
if (firstLineHasBeenWritten)
aString = newLine+aString;
//end if
else
firstLineHasBeenWritten = true;
//end else
writeLine(aString);
eof = false;
}//end writeString
/** Writes a int value as a new line into the sequential file.
*
* @param anInt which value will be written.
*/
public void writeInt(int anInt)
{
String line;
//------------
// se obtiene el String del int
line = Integer.toString(anInt);
writeLine(line);
eof = false;
}//end writeInt
/** Writes a double value as a new line into the sequential file.
*
* @param aDouble which value will be written.
*/
public void writeDouble(double aDouble)
{
String line;
//-------------
// se obtiene el double del String
line = Double.toString(aDouble);
line = line + newLine;
writeLine(line);
eof = false;
}//end writeDouble
/** Writes a char value as a new line into the sequential file.
*
* @param aChar which value will be written.
*/
public void writeChar(char aChar)
{
String line;
//-----------
// se inicia la cadena vac�a
line = "";
// se hace la cadena con el caracter
line = line + aChar + newLine;
writeLine(line);
eof = false;
}//end writeChar
/** Writes a boolean value as a new line into the sequential file.
*
* @param aBoolean which value will be written.
*/
public void writeBoolean(boolean aBoolean)
{
String line;
//------------
// se obtiene el boolean del String
line = Boolean.toString(aBoolean);
line = line + newLine;
writeLine(line);
eof = false;
}//end writeBoolean
}//end class UdlapSequentialFile