java - JFileChooser getCurrentDirectory() 到 String

标签 java string swing filenames jfilechooser

我有这个代码:

JFileChooser openFolder = new JFileChooser();
openFolder.setCurrentDirectory(new java.io.File("."));
openFolder.setDialogTitle("Select target directory");
openFolder.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
openFolder.setAcceptAllFileFilterUsed(false);
if (openFolder.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{      
    File newLoc = openFolder.getCurrentDirectory();   
}

我怎样才能使它转换:

    File newLoc = openFolder.getCurrentDirectory();

如果可能的话,转换为字符串?

例如,使用 FileChooser 我选择了文件夹:C:\Music

我尝试使用:

String locToString = FileUtils.readFileToString(newLoc);

但它不起作用。

我想将其转换为字符串,以便我可以使用以下方法将其显示在 JTextField 上:

jTextField.setText(locToString);

最佳答案

newLoc.getAbsolutePath() 将为您提供文件中的字符串,根据 javadoc .

关于java - JFileChooser getCurrentDirectory() 到 String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20958668/

相关文章:

java - 定期调用Java Web服务方法

java - ClassCastException : java. lang.String 无法转换为 Ljava.lang.String

c++ - 将 char 添加到字符串会导致 C++ 崩溃

Java Tetris - 奇怪的行清除问题

java - 键盘轮询有什么用?

java - 无法在 Android keystore 中生成 key

java - Java中 "if"和 "if else"的不同性能

python - 在Python中从一行文本中分割或去除可变数量的字符?

android - 如何在 android 中按 String 对 HashMap<String, String[]> 进行排序

java - JTabbedPane:从 Tab 内更改标题