-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsvReader.java
More file actions
65 lines (57 loc) · 1.95 KB
/
tsvReader.java
File metadata and controls
65 lines (57 loc) · 1.95 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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class tsvReader implements FileReader{
@Override
public void read() {
System.out.println("Reading from TSV file");
Scanner sc = null;
try {
sc = new Scanner(new File("/Users/kunal.singh/Downloads/demo/src/main/java/tsvDemo.tsv"));
sc.useDelimiter("\t");
while (sc.hasNext()) {
System.out.print(sc.next() + " ");
}
sc.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
@Override
public void write() {
System.out.println("Writing to TSV file");
String[][] data = {
{"Name", "Age", "Country"},
{"John", "25", "USA"},
{"Alice", "30", "UK"},
{"Bob", "22", "Canada"}
};
FileWriter writer = null;
try {
writer = new FileWriter(new File("/Users/kunal.singh/Downloads/demo/src/main/java/tsvDemo.tsv"));
for (String[] datum : data) {
for (int j = 0; j < datum.length; j++) {
writer.write(datum[j]);
if (j < datum.length - 1) {
writer.write("\t");
}
}
writer.write("\n");
}
System.out.println("Data written successfully to TSV file");
} catch (IOException e) {
System.out.println("An error occurred while writing to TSV file: " + e.getMessage());
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
System.out.println("An error occurred while writing to TSV file: " + e.getMessage());
}
}
}
}