选择文件的简单方法
Simple way to choose a file
在Java中有选择文件路径的简单方法吗?我一直在四处寻找,JFileChooser
不断出现,但这对我现在想要的来说已经太过分了,因为它似乎需要为此制作一个完整的 GUI。如果需要我会这样做,但是有没有更简单的方法来获取文件路径?
我想知道是否有类似 JOptionPane
对话框的东西来搜索文件路径。
这是在 Java 中选择文件路径的最简单方法:
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(YourClass.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
例如,您可以将此 actionPerformed 插入按钮,仅此而已。您的按钮将打开 select 文件的 GUI,如果用户 select 文件 JFileChooser.APPROVE_OPTION
,则您可以执行所需的操作(此处仅记录打开的内容)
如果您想执行其他操作(不绑定按钮?)或更复杂的操作(仅过滤某些扩展?...),请参阅 oracle 文档 (https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html)
当你周围没有UI时,你可以简单地使用这个(基于的答案)
public static void main( String[] args ) throws Exception
{
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog( null );
switch ( returnValue )
{
case JFileChooser.APPROVE_OPTION:
System.out.println( "chosen file: " + fileChooser.getSelectedFile() );
break;
case JFileChooser.CANCEL_OPTION:
System.out.println( "canceled" );
default:
break;
}
}
如果您只需要选择一个文件,JFileChooser 并不复杂。
public class TestFileChooser extends JFrame {
public void showFileChooser() {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println("Selected file: " + selectedFile.getAbsolutePath());
}
}
public static void main(String args[]) {
new TestFileChooser().showFileChooser();
}
}
在Java中有选择文件路径的简单方法吗?我一直在四处寻找,JFileChooser
不断出现,但这对我现在想要的来说已经太过分了,因为它似乎需要为此制作一个完整的 GUI。如果需要我会这样做,但是有没有更简单的方法来获取文件路径?
我想知道是否有类似 JOptionPane
对话框的东西来搜索文件路径。
这是在 Java 中选择文件路径的最简单方法:
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(YourClass.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
} ...
}
例如,您可以将此 actionPerformed 插入按钮,仅此而已。您的按钮将打开 select 文件的 GUI,如果用户 select 文件 JFileChooser.APPROVE_OPTION
,则您可以执行所需的操作(此处仅记录打开的内容)
如果您想执行其他操作(不绑定按钮?)或更复杂的操作(仅过滤某些扩展?...),请参阅 oracle 文档 (https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html)
当你周围没有UI时,你可以简单地使用这个(基于
public static void main( String[] args ) throws Exception
{
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog( null );
switch ( returnValue )
{
case JFileChooser.APPROVE_OPTION:
System.out.println( "chosen file: " + fileChooser.getSelectedFile() );
break;
case JFileChooser.CANCEL_OPTION:
System.out.println( "canceled" );
default:
break;
}
}
如果您只需要选择一个文件,JFileChooser 并不复杂。
public class TestFileChooser extends JFrame {
public void showFileChooser() {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println("Selected file: " + selectedFile.getAbsolutePath());
}
}
public static void main(String args[]) {
new TestFileChooser().showFileChooser();
}
}