我使用.exists
命令创建了一个覆盖文件选项。
但它在选定的目标路径中再次保存相同的文件!
File f = new File(destPathStr);
File[] destFilesList = f.listFiles();
if (f.exists()) {
int response = JOptionPane.showConfirmDialog(null, //
"Do you want to replace the existing file?", //
"Confirm", JOptionPane.YES_NO_OPTION, //
JOptionPane.QUESTION_MESSAGE);
if (response != JOptionPane.YES_OPTION) {
return duplicate;
}
}
我将文件存储在名为“f”的变量中。
但是在我的代码中,它无法正常工作,我无法在哪里制作它。
我的代码段上面的部分来自:
public boolean checkFileDuplicate(String destPathStr) //line 273
这是我的完整代码:
public class swingfinal implements ActionListener {
JFrame frame;
private JLabel lblSourceFolderfiles;
private JButton btnChoosedirectoryfrom;
private JLabel lblListFilesBelow;
private JButton btnDisplay;
private JTextField destPath;
private JTextField searchBox;
private JCheckBox chckbxSelectAll;
private JLabel lblNoOfFiles;
private JTextField noOfFileTxt;
private JButton btnGenerate;
private JLabel lblDestinationFolderTo;
private JButton btnBrowse;
private JPanel middlePanel;
private JScrollPane scrollPane;
private JFileChooser fileChooser;
//Objects
private List<Box> boxList = new ArrayList<Box>();
private List<JCheckBox> checkBoxList = new ArrayList<JCheckBox>();
private Map<String, String> fileNamesMap = new HashMap<String, String>();
//Variables
private int selectedCounter;
public swingfinal() {
frame = new javax.swing.JFrame( "Search Box" );
frame.getContentPane().setLayout( null );
frame.setSize( 1200, 720 );
frame.setVisible(true);
lblSourceFolderfiles = new JLabel("Source Folder/ Files");
lblSourceFolderfiles.setBounds(6, 17, 138, 14);
frame.getContentPane().add(lblSourceFolderfiles);
btnChoosedirectoryfrom = new JButton("ChooseDirectory From");
btnChoosedirectoryfrom.addActionListener(this);
btnChoosedirectoryfrom.setBounds(141, 9, 170, 30);
frame.getContentPane().add(btnChoosedirectoryfrom);
lblListFilesBelow = new JLabel("List files Below to choose ");
lblListFilesBelow.setBounds(344, 17, 180, 14);
frame.getContentPane().add(lblListFilesBelow);
btnDisplay = new JButton("Select To Display");
btnDisplay.setEnabled(false);
btnDisplay.setBounds(534, 9, 180, 30);
btnDisplay.addActionListener( this );
frame.getContentPane().add(btnDisplay);
searchBox = new JTextField();
searchBox.setBounds( 25, 50, 750, 40 );
searchBox.setFont( new Font( "Latha", Font.BOLD, 20 ) );
searchBox.setHorizontalAlignment( JTextField.CENTER );
frame.getContentPane().add( searchBox );
middlePanel = new JPanel ();
middlePanel.setBorder ( new TitledBorder ( new EtchedBorder (), "Display Area" ) );
middlePanel.setBounds(25, 110, 1100, 400);
chckbxSelectAll = new JCheckBox("Select All");
chckbxSelectAll.setBounds(25, 557, 97, 23);
chckbxSelectAll.addActionListener(this);
frame.getContentPane().add(chckbxSelectAll);
lblNoOfFiles = new JLabel("NO of Files Selected");
lblNoOfFiles.setBounds(41, 607, 139, 14);
frame.getContentPane().add(lblNoOfFiles);
noOfFileTxt = new JTextField();
noOfFileTxt.setBounds(163, 597, 125, 35);
frame.getContentPane().add(noOfFileTxt);
noOfFileTxt.setColumns(10);
btnGenerate = new JButton("Generate");
btnGenerate.setBounds(371, 584, 89, 60);
btnGenerate.addActionListener(this);
frame.getContentPane().add(btnGenerate);
lblDestinationFolderTo = new JLabel("Select Destination Path To Generate Files");
lblDestinationFolderTo.setBounds(553, 571, 259, 14);
frame.getContentPane().add(lblDestinationFolderTo);
btnBrowse = new JButton("Browse");
btnBrowse.setBounds(822, 567, 89, 23);
btnBrowse.addActionListener(this);
frame.getContentPane().add(btnBrowse);
destPath = new JTextField();
destPath.setBounds( 553, 600, 400, 30 );
destPath.setFont( new Font( "Latha", Font.BOLD, 20 ) );
frame.getContentPane().add( destPath );
}
@Override
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("ChooseDirectory From")) {
chooseDirectoryFrom();
} else if (ae.getActionCommand().equals("Select To Display")) {
selectToDisplay();
} else if(ae.getActionCommand().equals("Select All")) {
selectAllMethod();
} else if(ae.getActionCommand().equals("Browse")) {
outputDirSelection();
} else if(ae.getActionCommand().equals("Generate")) {
generate();
}
}
public void chooseDirectoryFrom() {
String tempStr = null;
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
fileChooser = new JFileChooser();
Font font = new Font("Latha", Font.ITALIC, 10);
fileChooser.setFont(new Font("Latha", Font.PLAIN, 13));
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setFont(font);
int returnVal = fileChooser.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
tempStr = fileChooser.getSelectedFile().getCanonicalPath();
}
if (tempStr != null && !tempStr.trim().equals("")) {
searchBox.setText(tempStr);
// Enable the search button
btnDisplay.setEnabled(true);
} else {
btnDisplay.setEnabled(false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void selectToDisplay() {
File sourceFolder = null;
Box box = Box.createVerticalBox();
if (boxList.size() != 0) {
middlePanel.remove(scrollPane);
middlePanel.repaint();
frame.repaint();
boxList = new ArrayList<Box>();
checkBoxList = new ArrayList<JCheckBox>();
fileNamesMap = new HashMap<String, String>();
selectedCounter = 0;
noOfFileTxt.setText(Integer.toString(selectedCounter));
}
sourceFolder = new File(searchBox.getText());
File[] sourceFilesList = sourceFolder.listFiles();
JCheckBox cb1 = null;
for (int i = 0; i < sourceFilesList.length; i++) {
if (sourceFilesList[i].isFile() & sourceFilesList[i].getName().endsWith(".txt")) {
fileNamesMap.put(sourceFilesList[i].getAbsolutePath(), sourceFilesList[i].getName());
cb1 = new JCheckBox(sourceFilesList[i].getAbsolutePath());
cb1.setFont(new Font("Latha", Font.BOLD, 20));
box.add(cb1);
checkBoxList.add(cb1);
cb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (((AbstractButton) e.getSource()).isSelected()) {
selectedCounter += 1;
} else {
selectedCounter -= 1;
if (selectedCounter < 0) {
selectedCounter = 0;
}
}
noOfFileTxt.setText(Integer.toString(selectedCounter));
}
});
}
}
boxList.add(box);
scrollPane = new JScrollPane(box);
scrollPane.setPreferredSize(new Dimension(1050, 350));
scrollPane.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
middlePanel.add ( scrollPane );
frame.getContentPane().add(middlePanel);
frame.repaint();
frame.revalidate();
}
public void selectAllMethod() {
Iterator<JCheckBox> i = checkBoxList.iterator();
while (i.hasNext()) {
JCheckBox tmp = i.next();
if (chckbxSelectAll.isSelected()) {
tmp.doClick();
} else {
tmp.setSelected(false);
selectedCounter -= 1;
if (selectedCounter < 0) {
selectedCounter = 0;
}
noOfFileTxt.setText(Integer.toString(selectedCounter));
}
}
}
public void generate() {
Iterator<JCheckBox> i = checkBoxList.iterator();
boolean once=false;
while(i.hasNext()) {
JCheckBox chkBox = i.next();
if(chkBox.isSelected()) {
String fileName = chkBox.getText();
String destPathStr = destPath.getText();
System.out.println("filepathname = " + fileName);
if(destPathStr != null) {
System.out.println("destPath: " + destPath.getText());
System.out.println("fileName: " + fileNamesMap.get(fileName));
boolean success = ex1final.exlCreator(fileName, destPath.getText(), fileNamesMap.get(fileName));
if(success&&!once) {
JOptionPane.showMessageDialog(null, "Successfully Completed. Pls refer to the path " + destPathStr + " for output files"); once=true;
} else if (once!=true) {
JOptionPane.showMessageDialog(null, "Some Error Occured!!!");once=true;
}
} else {
JOptionPane.showMessageDialog(null, "OOPS.You have to set the destination path!!!");
}
}
}
}
public void outputDirSelection() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
// handle exception
}
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = chooser.showSaveDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
destPath.setText(chooser.getSelectedFile().getAbsolutePath());
}
}
public boolean checkFileDuplicate(String destPathStr) {
boolean duplicate = false;
File f = new File(destPathStr);
File[] destFilesList = f.listFiles();
if (f.exists()) {
int response = JOptionPane.showConfirmDialog(null, //
"Do you want to replace the existing file?", //
"Confirm", JOptionPane.YES_NO_OPTION, //
JOptionPane.QUESTION_MESSAGE);
if (response != JOptionPane.YES_OPTION) {
return duplicate;
}
}
for(File f1:destFilesList) {
duplicate = fileNamesMap.containsKey(f1.getName());
if(duplicate == true) {
return duplicate;
}
}
f = null;
destFilesList = null;
return duplicate;
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new swingfinal();
}
});
}
}