Support Questions
Find answers, ask questions, and share your expertise

Can't rename file in custom nifi code

I have this code inside my onTrigger method in order to rename file and then put it in folder but for unknown reasons i can't do it in log file i can't see any failure, error or warn. Mainly i want to make custom processor which get xml file from folder and then update its data, after all sends this data to flowfile attributes and transfers this newly created flowfile. then i wnat to update renew data in config.xml file and send it back to folder but i cant do this , i can send ".conf.xml" in folder but i can't rename it to "conf.xml".what should i change to make this code work?

 final Path rootDirPath = get("C://Users//user//Desktop//try2//nifi-1.3.0//1");
                final Path tempCopyFile = rootDirPath.resolve("."+flowFile.getAttribute(CoreAttributes.FILENAME.key()));
                final Path dotCopyFile =tempCopyFile;
                Path tempDotCopyFile = null;
                tempDotCopyFile = dotCopyFile;
                final Path copyFile=rootDirPath.resolve(flowFile.getAttribute(CoreAttributes.FILENAME.key()));
                Path finalCopyFile = copyFile;
                if (!Files.exists(rootDirPath)) {
                    if (true) {
                        Files.createDirectories(rootDirPath);
                    }
                }
                final Path finalCopyFileDir = finalCopyFile.getParent();
                if (Files.exists(finalCopyFileDir)) { // check if too many files already
                    final int numFiles = finalCopyFileDir.toFile().list().length;
                    if (numFiles >= 10) {
                        flowFile = session.penalize(flowFile);
                        logger.warn("Penalizing {} and routing to 'failure' because the output directory {} has {} files, which exceeds the "
                                + "configured maximum number of files", new Object[]{flowFile, finalCopyFileDir, numFiles});
                        session.transfer(flowFile, REL_FAILURE);
                        return;
                    }
                }
                session.exportTo(flowFile,dotCopyFile,false);
                //dotCopyFile.toFile().renameTo(finalCopyFile.toFile());
                logger.info("added {} to flow", new Object[]{flowFile,flowFile1});
                boolean renamed = false;
                for (int i = 0; i < 10; i++) { // try rename up to 10 times.
                    if (dotCopyFile.toFile().renameTo(finalCopyFile.toFile())) {
                        renamed = true;
                        break;// rename was successful
                    }
                    Thread.sleep(100L);// try waiting a few ms to let whatever might cause rename failure to resolve
                }
                if (!renamed) {
                    if (Files.exists(dotCopyFile) && dotCopyFile.toFile().delete()) {
                        logger.debug("Deleted dot copy file {}", new Object[]{dotCopyFile});
                    }
                    throw new ProcessException("Could not rename: " + dotCopyFile);
                } else {
                    logger.info("Produced copy of {} at location {}", new Object[]{flowFile, finalCopyFile});
                }
0 REPLIES 0