[英]JFileChooser and copying files
我正在上课作业,并且有几个问题希望得到帮助。 分配是一个GUI,允许用户选择要复制的文件并选择要将文件复制到的位置。
我已经完成任务,但有几件事我想看看我是否可以改变...
当选择源文件时,我只希望源文件的名称显示在标签中,但是,该程序需要整个路径才能复制文件,并且每次我尝试切换它以仅显示文件名时程序将无法运行,因为它不知道文件的位置。 第二个问题,是否仍然要使程序自动将文件复制为.bak文件...说源文件是文本文件,用户选择目标文件夹并点击复制,然后保存一个同名文件,但是.bak扩展名?
我将有问题的代码放在***之间,并留下了我试图用来仅显示文件名的代码并注释掉了。 谢谢你的帮助!!
public class CopyFile extends JFrame{
private JFileChooser fc;
private JButton copyButton;
private JButton chooseFileButton;
private JButton destinationButton;
private File workingDirectory;
private JLabel sourceLabel;
private JLabel destinationLabel;
private JTextField sourceText;
private JTextField sourceFileText;
private JTextField destinationText;
public static void main(String [] args) {
CopyFile go = new CopyFile();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(500, 150);
go.setVisible(true);
}
public CopyFile() {
super("Copy a text file");
setLayout(new GridLayout(3, 3, 5, 5));
fc = new JFileChooser();
//Open dialog box inside project folder to make easier to find files
workingDirectory = new File(System.getProperty("user.dir"));
fc.setCurrentDirectory(workingDirectory);
//create labels and buttons for window
chooseFileButton = new JButton("CHOOSE SOURCE FILE");
destinationButton = new JButton("DESTINATION FOLDER");
copyButton = new JButton("COPY FILE");
sourceLabel = new JLabel("SOURCE FILE: ");
sourceText = new JTextField(10);
sourceText.setEditable(false);
destinationLabel = new JLabel("DESTINATION: ");
destinationText = new JTextField(10);
//add everything to JFrame
add(sourceLabel);
add(sourceText);
add(chooseFileButton);
add(destinationLabel);
add(destinationText);
add(destinationButton);
add(copyButton);
//Create TheHandler object to add action listeners for the buttons.
TheHandler handler = new TheHandler();
chooseFileButton.addActionListener(handler);
destinationButton.addActionListener(handler);
copyButton.addActionListener(handler);
}
//Inner class to create action listeners
private class TheHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
int returnVal;
String selectedFilePath;
File selectedFile;
******************************************************************************
//Selecting a source file and displaying what the user is doing.
if(event.getSource() == chooseFileButton) {
returnVal = fc.showOpenDialog(null);
//Set the path for the source file.
if(returnVal == JFileChooser.APPROVE_OPTION) {
/*The two next lines of code are what I was trying to do to get only the
file name but I get a whole page of errors, mainly I think it's saying no
such file exists*/
//selectedFile = fc.getSelectedFile();
//sourceText.setText(selectedFile.getName());
selectedFilePath = fc.getSelectedFile().getAbsolutePath();
sourceText.setText(selectedFilePath);
}
}//end if
******************************************************************************
//Handle destination button.
if(event.getSource() == destinationButton) {
returnVal = fc.showSaveDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
selectedFilePath = fc.getSelectedFile().getAbsolutePath();
destinationText.setText(selectedFilePath);
}
}//end if
//Handle copy button
if(event.getSource() == copyButton) {
File sourceFile = new File(sourceText.getText());
File destinationFile = new File(destinationText.getText());
Path sourcePath = sourceFile.toPath();
Path destinationPath = destinationFile.toPath();
try {
Files.copy(sourcePath, destinationPath);
} catch (IOException e) {
e.printStackTrace();
}
}//end if
}//end actionPerformed
}//end TheHandler class
}//end class
当选择源文件时,我只希望源文件的名称显示在标签中,但是,该程序需要整个路径才能复制文件,并且每次我尝试切换它以仅显示文件名时程序将无法运行,因为它不知道文件的位置。
您可以使用File#getName
,它将返回文件名并将其用作标签的文本,但保留对原始File
的引用。 您不应该使用标签的文本来生成新的File
引用,而只需保留对源File
和目标File
以及实例字段的引用
第二个问题,是否仍然要使程序自动将文件复制为.bak文件...说源文件是文本文件,用户选择目标文件夹并点击复制,然后保存一个同名文件,但是.bak扩展名?
String name = selectedFile.getName();
name = name.substring(0, name.lastIndexOf("."));
name += ".bak";
File destinationFile = new File(destinationPath, name);
会将selectedFile
的扩展名更改为.bak
,但是您可能应该添加检查以查看其是否以扩展名开头
您必须将源文件路径和目标文件路径保留为
File
,而不是String
。 如下修改TheHandler
类的代码。
添加selectedSourceFile
和selectedDestinationFile
本地字段。
private class TheHandler implements ActionListener { private File selectedSourceFile; private File selectedDestinationFile;
选择文件时更新它们,并设置文件名而不是文本字段的路径。
源文件按钮
if (event.getSource() == chooseFileButton) {
returnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
selectedSourceFile = fc.getSelectedFile();
sourceText.setText(selectedSourceFile.getName());
}
}
目标按钮
if (event.getSource() == destinationButton) {
returnVal = fc.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
selectedDestinationFile = fc.getSelectedFile();
destinationText.setText(selectedDestinationFile.getName());
}
}
复制它们时,请使用selectedSourceFile
和selectedDestinationFile
。
if (event.getSource() == copyButton) { Path sourcePath = selectedSourceFile.toPath(); Path destinationPath = selectedDestinationFile.toPath(); try { Files.copy(sourcePath, destinationPath); } catch (IOException e) { e.printStackTrace(); } }
现在,您已经完成了第一个要求。 您可以在选择目标文件时制作backfile。 因此,在选择目标按钮时,添加您的代码以制作备份文件。
if (event.getSource() == destinationButton) {
returnVal = fc.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
selectedDestinationFile = fc.getSelectedFile();
destinationText.setText(selectedDestinationFile.getName());
//copy backup
String name = selectedSourceFile.getName();
name = selectedSourceFile.getName().substring(0, name.lastIndexOf(".")) + ".bak";
File destinationFile = new File(selectedDestinationFile.getParentFile(), name);
try {
Files.copy(selectedSourceFile.toPath(), destinationFile.toPath());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.