-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadFromFile.java
41 lines (33 loc) · 1.06 KB
/
ReadFromFile.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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class ReadFromFile {
/** Reads data from file */
public static String[][] read(String file){
File myCSVfile = new File(file);
String line = "";
String CSVsplitter = ","; // comma as seperator
int i = 0;
String[][] tab;
try (BufferedReader br = new BufferedReader(new FileReader(myCSVfile))) {
while(br.readLine() != null) {
i++;
}
} catch (IOException e) {
e.printStackTrace();
}
tab = new String[i][i];
try (BufferedReader br = new BufferedReader(new FileReader(myCSVfile))) {
int j = 0;
while ((line = br.readLine()) != null && j < tab.length) {
String[] lineTokens = line.split("\n");
tab[j] = lineTokens[0].split(CSVsplitter);
j++;
}
} catch (IOException e) {
e.printStackTrace();
}
return tab;
}
}