-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathfileCreate.java
112 lines (83 loc) · 2.29 KB
/
fileCreate.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
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.NumberFormatException;
class AddFriend {
public static void main(String data[])
{
try {
// Get the name of the contact to be updated
// from the Command line argument
String newName = data[0];
// Get the number to be updated
// from the Command line argument
long newNumber = Long.parseLong(data[1]);
String nameNumberString;
String name;
long number;
int index;
// Using file pointer creating the file.
File file = new File("friendsContact.txt");
if (!file.exists()) {
// Create a new file if not exists.
file.createNewFile();
}
// Opening file in reading and write mode.
RandomAccessFile raf
= new RandomAccessFile(file, "rw");
boolean found = false;
// Checking whether the name
// of contact already exists.
// getFilePointer() give the current offset
// value from start of the file.
while (raf.getFilePointer() < raf.length()) {
// reading line from the file.
nameNumberString = raf.readLine();
// splitting the string to get name and
// number
String[] lineSplit
= nameNumberString.split("!");
// separating name and number.
name = lineSplit[0];
number = Long.parseLong(lineSplit[1]);
// if condition to find existence of record.
if (name == newName
|| number == newNumber) {
found = true;
break;
}
}
if (found == false) {
// Enter the if block when a record
// is not already present in the file.
nameNumberString
= newName + "!"
+ String.valueOf(newNumber);
// writeBytes function to write a string
// as a sequence of bytes.
raf.writeBytes(nameNumberString);
// To insert the next record in new line.
raf.writeBytes(System.lineSeparator());
// Print the message
System.out.println(" Friend added. ");
// Closing the resources.
raf.close();
}
// The contact to be updated
// could not be found
else {
// Closing the resources.
raf.close();
// Print the message
System.out.println(" Input name"
+ " does not exists. ");
}
}
catch (IOException ioe) {
System.out.println(ioe);
}
catch (NumberFormatException nef) {
System.out.println(nef);
}
}
}