-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUseCSV.java
More file actions
85 lines (70 loc) · 2.52 KB
/
UseCSV.java
File metadata and controls
85 lines (70 loc) · 2.52 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
75
76
77
78
79
80
81
82
83
84
85
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import musica.Crud;
import musica.Musica;
class UseCSVReader {
private String file;
private String line;
public UseCSVReader(){
file = "db" + File.separator + "spotifyTeste.csv";
line = "";
}
public void readFile() throws Exception{
String name, key;
ArrayList<String> artists;
int id, duration_ms, explicit;
double tempo;
Date release_date;
FileReader fr = new FileReader(this.file);
BufferedReader br = new BufferedReader(fr);
line = br.readLine();
while(line != null){
String[] field = line.split(";");
id = Integer.parseInt(field[0]);
key = field[1];
name = field[2];
duration_ms = Integer.parseInt(field[3]);
explicit = Integer.parseInt(field[4]);
// arraylist
String artists_string = field[5];
artists_string = artists_string.replace("[", "");
artists_string = artists_string.replace("]", "");
artists_string = artists_string.replace("'", "");
String[] artists_field = artists_string.split(",");
artists = new ArrayList<String>();
for(int i = 0; i < artists_field.length; i++){
artists.add(artists_field[i]);
}
// tratar data -> os que tiverem apenas ano, colocar -01-01 no final
String release_date_string = field[6];
if (release_date_string.contains("/")){
SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
release_date = formato.parse(release_date_string);
}
else {
if (release_date_string.length() == 4) release_date_string += "-01-01";
if (release_date_string.length() == 7) release_date_string += "-01";
SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd");
release_date = formato.parse(release_date_string);
}
String tempo_string = field[7].replace(',', '.');
tempo = Double.parseDouble(tempo_string);
Crud crud = new Crud();
Musica musica = new Musica(id, key, name, artists, duration_ms, explicit, tempo, release_date);
crud.create(musica);
System.out.println(musica.toString());
line = br.readLine();
}
br.close();
}
}
public class UseCSV {
public static void main(String[] args) throws Exception {
UseCSVReader csv = new UseCSVReader();
csv.readFile();
}
}