-
Notifications
You must be signed in to change notification settings - Fork 0
/
DictionaryDemo.java
72 lines (55 loc) · 1.89 KB
/
DictionaryDemo.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
import java.util.*;
import java.io.*;
public class DictionaryDemo
{
public static void main(String[] args)
{
Map<String, String> map = new HashMap<String, String>();
map.put("2206", "Rahul");
map.put("2211", "Shubham");
map.put("2709", "Yash");
map.put("1234", "Deep");
Scanner sc=new Scanner(System.in);
System.out.println("Enter your ID");
String name=sc.next();
System.out.println("Hey Yo, "+map.get(name));
String fileName = "chat.txt";
try
{
FileWriter fileWriter = new FileWriter(fileName);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
System.out.println("Message :");
String mess=sc.next();
bufferedWriter.write(map.get(name));
bufferedWriter.write(" :");
bufferedWriter.write(mess);
bufferedWriter.newLine();
bufferedWriter.close();
}
catch(IOException ex)
{
System.out.println("Error writing to file '"+ fileName + "'");
}
String line = null;
try {
FileReader fileReader =
new FileReader(fileName);
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
}
}
}