-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileSelector.java
56 lines (45 loc) · 1.51 KB
/
FileSelector.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
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFileChooser;
public class FileSelector {
public String selectFile() throws IOException {
String result = null;
try {
JFileChooser loadEmp = new JFileChooser();// new dialog
loadEmp.setDialogTitle("Seleccionar fichero de entrada");
File selectedFile = null;
FileReader reader = null;
if (loadEmp.showOpenDialog(loadEmp) == JFileChooser.APPROVE_OPTION) {
selectedFile = loadEmp.getSelectedFile();
if (selectedFile.canRead() && selectedFile.exists()) {
reader = new FileReader(selectedFile);
}
result = selectedFile.getPath();
}
} catch (NullPointerException ex) {
System.out.println("Open File Cancelled:\n" + ex.getMessage()
+ "\n");
}
return result;
}
public String selectDirectory() {
String result = null;
try{
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Seleccionar directorio");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
// System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
// System.out.println("getSelectedFile() : " + chooser.getSelectedFile());
result = chooser.getSelectedFile().getPath();
} else {
System.out.println("No Selection ");
}
}catch(Exception e){
}
return result;
}
}