String currentDir = System.getProperty("user.dir");
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Text files", "txt");
chooser.setFileFilter(filter);
chooser.setCurrentDirectory(currentDir);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
String fileName = chooser.getSelectedFile().getName();
System.out.println("You chose to open this file: " +
fileName);
Snažím sa modifikovať JFileChooser,Príklad ShowOpenDialog na použitie aktuálneho adresára balíka java pomocou System.getProperty ("user.dir"), ktorý vracia reťazec, ale setCurrentDirectory (File) chce objekt File. Ako môžem previesť reťazec na súbor? Vďaka.
odpovede:
1 pre odpoveď č. 1Jeho relatívne ľahké ...
File file = new File("example_file.txt");
0 pre odpoveď č. 2
Môžeš použiť
String currentDir = System.getProperty("user.dir");
....
chooser.setCurrentDirectory(new File(currentDir));
-1 pre odpoveď č. 3
Ako môžem previesť reťazec na súbor?
A čo toto?
String str = "C:1.txt";
File file = Paths.get(str).toFile();