将“getSelectedFile”写入字符串(Java)
这可能是一个措词不当的问题,但我们现在开始: 我需要这部分代码来生成“fileChooser.getSelectedFile;”可以在其他地方使用的格式。我不介意它是否是一个变量,因为我需要在另一个actionListener中调用它,或者按照计划将选定文件夹作为字符串输出到输出文件,然后在程序的其他地方读取该文件 文件路径(例如C:/Users/Desktop/)必须是字符串,这一点很重要,因为使用该路径的类将接受该字符串 我尝试了几个选项,但经常会遇到不可逆类型的编译错误等,如果有人有任何想法,他们会分享,那就太好了将“getSelectedFile”写入字符串(Java),java,file-io,jfilechooser,Java,File Io,Jfilechooser,这可能是一个措词不当的问题,但我们现在开始: 我需要这部分代码来生成“fileChooser.getSelectedFile;”可以在其他地方使用的格式。我不介意它是否是一个变量,因为我需要在另一个actionListener中调用它,或者按照计划将选定文件夹作为字符串输出到输出文件,然后在程序的其他地方读取该文件 文件路径(例如C:/Users/Desktop/)必须是字符串,这一点很重要,因为使用该路径的类将接受该字符串 我尝试了几个选项,但经常会遇到不可逆类型的编译错误等,如果有人有任何想
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){
System.out.println(fileChooser.getSelectedFile());
}
}
});
似乎可以解决它。很抱歉发布并回答我自己的问题。在我等待回复的时候,我知道怎么做了。似乎是一个字符串“exportPath”必须包含一个字符串。在这种情况下\但也可能是太多
不知道为什么,但你看,D
似乎可以解决它。很抱歉发布并回答我自己的问题。在我等待回复的时候,我知道怎么做了。似乎是一个字符串“exportPath”必须包含一个字符串。在这种情况下\但也可能是太多
不知道为什么,但你可以这样做:D很少有可能做到这一点:
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){
}
}
}
});
这样做的可能性很小:
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){
}
}
}
});
JFileChoose.getSelectedFile返回文件对象,而不是字符串对象 文件对象具有诸如getAbsolutePath、getPath、getName、getParent之类的方法,这些方法返回文件名和路径的字符串版本 比如:
// 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();
你应该得到你想要的
另外,仅供参考,本文件未编译
File file = fileChooser.getSelectedFile();
System.out.println("Selected file is: "+file.getAbsolutePath()+"/"+file.getName());
。。。因为getSelectedFile返回的文件对象不是字符串对象。但是,与所有对象一样,File对象也有一个toString方法,当您执行此操作时,会自动调用该方法来构建字符串
String exportPath = fileChooser.getSelectedFile();
正如我所说,优雅的方式应该是这样的:
String exportPath = fileChooser.getSelectedFile() +"\\";
希望这有帮助,祝你好运!
RobJFileChoose.getSelectedFile返回文件对象,而不是字符串对象 文件对象具有诸如getAbsolutePath、getPath、getName、getParent之类的方法,这些方法返回文件名和路径的字符串版本 比如:
// 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();
你应该得到你想要的
另外,仅供参考,本文件未编译
File file = fileChooser.getSelectedFile();
System.out.println("Selected file is: "+file.getAbsolutePath()+"/"+file.getName());
。。。因为getSelectedFile返回的文件对象不是字符串对象。但是,与所有对象一样,File对象也有一个toString方法,当您执行此操作时,会自动调用该方法来构建字符串
String exportPath = fileChooser.getSelectedFile();
正如我所说,优雅的方式应该是这样的:
String exportPath = fileChooser.getSelectedFile() +"\\";
希望这有帮助,祝你好运!
罗伯我在这里真的很困惑。您希望它作为字符串,但不希望它作为变量,因为您需要调用它?你怎么能调用字符串?如果需要字符串,请使用字符串变量;如果需要调用某个对象,请使用接口。你到底想在这里实现什么?你可以发布一个伪代码的例子,它使用你在这里试图创建的任何东西吗?基本上,我希望“getSelectedFile”是一个字符串类型,而不是file.io类型。File.io类型不会写入输出文件,但我知道字符串可以。我在下面回答了我自己的问题,如果你想看看我是怎么做到的,谢谢你的回答。谢尔盖,很高兴知道别人需要帮助的时候人们都在说:我真的很困惑。您希望它作为字符串,但不希望它作为变量,因为您需要调用它?你怎么能调用字符串?如果需要字符串,请使用字符串变量;如果需要调用某个对象,请使用接口。你到底想在这里实现什么?你可以发布一个伪代码的例子,它使用你在这里试图创建的任何东西吗?基本上,我希望“getSelectedFile”是一个字符串类型,而不是file.io类型。File.io类型不会写入输出文件,但我知道字符串可以。我在下面回答了我自己的问题,如果你想看看我是怎么做的,谢谢你的回答,但是Sergey,很高兴知道人们在什么时候需要帮助:当你向某个不是字符串的对象添加字符串时,会隐式调用toString方法。这就是它起作用的原因。请参阅我的答案,了解以更具可读性的方式实现相同功能的其他方法。当您向非字符串的对象添加字符串时,将隐式调用toString方法。这就是它起作用的原因。请参阅我的答案,了解以更具可读性的方式实现相同目标的其他方法。