1

I am using JFileChooser to generate PDF file in the specified file location but my requirement is this when we generated PDF like d:\\test.pdf** in the d drive location and we again try to generated the same PDF file **it is override the previous PDF file . The requirement is this they show message box to show it is already generated and generate the other PDF file name.like test1.pdf my question Code: apply for the button

    JFileChooser dialog = new JFileChooser();
//            chooser.setDialogType(JFileChooser.SAVE_DIALOG);
        dialog.setCurrentDirectory(new java.io.File("."));
        dialog.setDialogTitle("Save Backup");
        dialog.setApproveButtonText("Save");
        //disables the all filesoptioning here
        dialog.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        dialog.setAcceptAllFileFilterUsed(false);

        if (dialog.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            System.out.println("getCurrentDirectory(): " + dialog.getCurrentDirectory());
            System.out.print("getSelectedFile() : " + dialog.getSelectedFile());

            try {
                String filePath = dialog.getSelectedFile().getPath();
                Document document = new Document();
                PdfWriter.getInstance(document, new FileOutputStream(filePath));
                document.open();
                document.add(new Paragraph(" hello"));
                document.close();
        } catch (Exception e) {

        }
    }
satish
  • 11
  • 4
  • Perhaps look at something like [`File.exists()`](http://docs.oracle.com/javase/8/docs/api/java/io/File.html#exists--). – Andrew Thompson Mar 01 '16 at 14:40
  • but sir my question is that how to find out to compare the location which is already generated pdf file and newly generated file with location. – satish Mar 01 '16 at 14:42
  • `File.equals(File)`? I'm having trouble understanding exactly what you mean. Perhaps it would be better to explain what happens from the user perspective. E.G. 1) User generates and saves a PDF 2) User creates a new PDF in the editor. 3) User goes to save new PDF over old PDF. 4) ..what should happen then? – Andrew Thompson Mar 01 '16 at 14:46
  • user Requirement is this first time when he create pdf using jfilechooser they given the name of pdf in specified location like d:\test.pdf after 5 min later if another user create the same name like d:\test.pdf then they show messagebox like joptionpane to show name is already exits please choose another name for the pdf. – satish Mar 01 '16 at 14:58

1 Answers1

0
if (dialog.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
    File selectedFile = dialog.getSelectedFile();

    if (selectedFile.exists()) {
        JOptionPane.showMessageDialog(this, "Please choose another file.");
        return;
    }

    PdfWriter.getInstance(document, new FileOutputStream(selectedFile));
    document.open();
    document.add(new Paragraph(" hello"));
    document.close();
}
andrucz
  • 1,971
  • 2
  • 18
  • 30
  • `JOptionPane.showMessageDialog(this, "Please choose another file."); return;` If I was the user I'd prefer to be prompted. Something more like.. `int result = JOptionPane.showConfirmDialog(this, "Sure you want to overwrite?"); if (result==JOptionPane.OK_OPTION) { .. } else { ..` – Andrew Thompson Mar 01 '16 at 15:05
  • Just wanted to show him how to check whether selected file exists in that context (which is the main question, I guess). – andrucz Mar 01 '16 at 15:08
  • but first how to check generated pdf file path and new create file path ,i am using if(chooserobj.getSelectedFile(),getselectedpath==???){joptionpane.showmessagedialog(this,"name is already exits please choose another name")} but this code is not working – satish Mar 01 '16 at 15:11
  • please help me related to this issues]' – satish Mar 01 '16 at 15:26
  • please send me the logic to find out this problem? – satish Mar 01 '16 at 16:00