user8653357 user8653357 - 1 year ago 93
Java Question

JAVA : Cannot copy the images from one folder to a word document located in different folder

I need to copy all the images located in a Folder to a word doc in a different folder(word doc is created during run time with the same name as srcfile). When i run the below code, No exceptions are seen but the images are not copied to word document. can someone please help me

Code:

File srcfile=new File("C:\\reports\\test"
+ fileName() + "\\images");
File destfile = new File("D:\\Evidences"+ "\\test" + fileName() + ".docx");

destfile.getParentFile().mkdir();
destfile.createNewFile();

File[] list = srcfile.listFiles();
int img=list.length;
System.out.println(img);
for(int k=0;k<list.length;k++){
if (list[k].isFile()){
System.out.println("File"+list[k].getName());
if(srcfile.isFile()){
FileInputStream fis = new FileInputStream(srcfile);
XWPFDocument doc = new XWPFDocument(fis);
XWPFParagraph p = doc.createParagraph();
XWPFRun r = p.createRun();
r.addPicture (fis, XWPFDocument.PICTURE_TYPE_PNG,
"C:\\reports\\test"+ fileName() + "\\images\\"+imageName() + ".png",
Units.toEMU(200), Units.toEMU(200));
fis.close();
FileOutputStream fos =new FileOutputStream(destfile);
doc.write(fos);
fos.close();
doc.close();

}
}
else if (list[k].isDirectory()){
System.out.println("Directory"+list[k].getName());
if(srcfile.isFile()){
FileInputStream fis = new FileInputStream(srcfile);
XWPFDocument doc = new XWPFDocument(fis);
XWPFParagraph p = doc.createParagraph();
XWPFRun r = p.createRun();
r.addPicture (fis, XWPFDocument.PICTURE_TYPE_PNG,
"C:\\reports\\test"+ fileName() + "\\images\\"+imageName() + ".png",
Units.toEMU(200), Units.toEMU(200));
fis.close();
FileOutputStream fos =new FileOutputStream(destfile);
doc.write(fos);
fos.close();
doc.close();
}
}
}


Console:

Picked up JAVA_TOOL_OPTIONS: -agentlib:jvmhook
Picked up _JAVA_OPTIONS: -Xrunjvmhook -Xbootclasspath/a:"C:\Program Files (x86)\HP\Unified Functional Testing\bin\java_shared\classes";"C:\Program Files (x86)\HP\Unified Functional Testing\\bin\java_shared\classes\jasmine.jar";"C:\Program Files\Java\jdk1.8.0_25\bin"
12
File0.PNG
File1.PNG
File10.PNG
File2.PNG
File3.PNG
File4.PNG
File5.PNG
File6.PNG
File7.PNG
File8.PNG
File9.PNG
Filetest463.docx

Answer Source

Along with what @sighil has suggested sorting the list helped me put things in sequence. Below is the final code:

public static void main(String[] args) throws IOException, 
InvalidFormatException {

// Create the destination file and folder structure
File destfile = new File("C:\\Evidences\\test\\imgdocx.docx");
destfile.getParentFile().mkdirs();
destfile.createNewFile();

// Create the docx object
XWPFDocument doc = new XWPFDocument();

// Get the source folder and list of files (includes images and
// sub-folders)
File srcfile = new File("C:\\reports\\test\\images");
File[] list = srcfile.listFiles();
Arrays.sort(list, Comparator.comparingLong(File::lastModified));
System.out.println("Source folder item list " + list.length);

// Iterate through the files in the source folder
for (int k = 0; k < list.length; k++) {
    if (list[k].isFile()) {
        System.out.println("File - " + list[k].getName());

        // Create paragraph
        XWPFParagraph p = doc.createParagraph();
        XWPFRun r = p.createRun();

        // Create fis for image
        FileInputStream fis = new FileInputStream(list[k].getPath());
        // add image using the fis
        r.addPicture(fis, XWPFDocument.PICTURE_TYPE_PNG,
                list[k].getPath(),
                Units.toEMU(200), Units.toEMU(200));
        fis.close();

    } else if (list[k].isDirectory()) {
        System.out.println("Directory - " + list[k].getName());
        // If directory do nothing
        // A recursive logic can be implemented to handle the files
        // inside folders in source folder
    }
}

// write docx to destination file
FileOutputStream fos = new FileOutputStream(destfile);
doc.write(fos);
fos.close();
doc.close();

}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download