-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRAF.java
More file actions
executable file
·74 lines (64 loc) · 1.62 KB
/
RAF.java
File metadata and controls
executable file
·74 lines (64 loc) · 1.62 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
/* Librarian Assistant Pro - Version 1.7
* Class: RAF (Random Access File)
* Duties:
* The instance of this class represents a RandomAccessFile
* the class is used to read data from and write data to this
* RandomAccessFile
* Creation Date: January 2009
*/
import java.io.RandomAccessFile;
import java.io.*;
import java.util.ArrayList;
class RAF
{
private RandomAccessFile raf;
public RAF(File file)
{
try
{
raf = new RandomAccessFile(file, "rw");
}
catch (IOException ex)
{
System.out.println("Error occured!");
}
}
public ArrayList<String> readRAF()
throws IOException, NullPointerException
{
ArrayList<String> stringArray = new ArrayList<String>();
//Making sure the pointing is set to the beginning of the File
raf.seek(0);
//Writing the arrayList of strings into the RandomAccessFile
while (raf.getFilePointer() < raf.length())
{
stringArray.add(raf.readUTF());
}
return stringArray;
}
public void writeRAF(ArrayList<String> stringArray)
throws IOException
{
raf.seek(0); //Making sure the pointing is set to the beginning of the File
//So that the data would be overwritten
try
{
int i = 0;
//Reading RandomAccessFile and
//Writing data into the arrayList of strings
while(i < stringArray.size())
{
raf.writeUTF(stringArray.get(i));
i++;
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}