open.addActionListener(new ActionListene开发者_如何转开发r(){
public void actionPerformed (ActionEvent e){
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new java.io.File("."));
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
System.out.println(fileChooser.getSelectedFile());
}
}
});
This might be a badly worded question but here we go:
I need this part of my code to produce 'fileChooser.getSelectedFile());' in a format that can be used elsewhere. I don't mind if it is a variable (won't really work because I need to call it in another actionListener) or (as I planned) output the selected folder as a string to an output file and then read that file in elsewhere in the program.
It's important that the file path (e.g. C:/Users/Desktop/) be a String because that is what the class which will use the path takes in.
I've tried a couple of options but often get the "inconvertable types' compile error etc, if anyone has any ideas they would share, that'd be great
JFileChoose.getSelectedFile() returns a File object, not a String object.
The File object has methods like getAbsolutePath(), getPath(), getName(), getParent() that return string versions of the file name and path.
So something like:
File file = fileChooser.getSelectedFile();
System.out.println("Selected file is: "+file.getAbsolutePath()+"/"+file.getName());
Should get you what you want.
Also FYI, this doesn't compile ...
String exportPath = fileChooser.getSelectedFile();
... because the File object returned by getSelectedFile() is not a String object. However, the File object (like all objects) has a toString() method which gets called automatically to build the string when you did this ...
String exportPath = fileChooser.getSelectedFile() +"\\";
The elegant way would be, as I said, something like this:
String exportPath = fileChooser.getSelectedFile().getAbsolutePath();
Hope this helps, good luck! Rob
There are few possibilities to do that:
// just path as a String
fileChooser.getSelectedFile().getPath();
// the same, this is done implicitly in your answer
fileChooser.getSelectedFile().toString();
// absolute path, if you need it
fileChooser.getSelectedFile().getAbsolutePath();
// canonical path, not sure what that is
fileChooser.getSelectedFile().getCanonicalPath();
open.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new java.io.File("."));
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
try{
DataOutputStream outputPath = new DataOutputStream(new FileOutputStream("C:/Users/Jonathan/Desktop/YouDetectJava/FolderPath.dat"));
String exportPath = fileChooser.getSelectedFile() + "\\";
outputPath.writeUTF(exportPath);
//System.out.println(exportPath);
}catch (IOException ioe){
}
}
}
});
Seems to fix it. Sorry to post and answer my own question. Figured out how to do it whilst I was waiting for replies. Seems like to be a string 'exportPath' has to have a string in it. In this case "\" but it can also be "" too.
No idea why but there you go :D
精彩评论