Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have file encoded in ISO-8859-1 I want to convert in UTF-8 before processing them.

I created a tasklet that read all files in my directory, and write it in a new file in this directory :

@Bean
public Tasklet convertFileToUtf8Tasklet() {
    return (stepContribution, chunkContext) -> {

        String basePath = securisationPaymentConfigProperties.getInputFileDirectory();

        try {
            File directory = new File(basePath);
            Collection<File> files = FileUtils.listFiles(directory,
                    new String[]{"xml", "XML"}, false);

            for(File file : files){
                log.info("Converting File "+file.getName());
                FileInputStream fis = new FileInputStream(file);
                InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.ISO_8859_1);
                Reader in = new BufferedReader(isr);

                File outputFile = new File(basePath + "UTF8-" + file.getName());
                FileOutputStream fos = new FileOutputStream(outputFile);
                OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
                Writer out = new BufferedWriter(osw);

                int ch;
                while ((ch = in.read()) > -1) {
                    out.write(ch);
                }

                out.close();
                in.close();

                Files.move(file.toPath(),
                        Path.of(securisationPaymentConfigProperties.getCompleteFolderPath() + file.getName()), REPLACE_EXISTING);
            }

        }catch (IOException e){
            log.error("Error Reading XML Files : "+e);
        }
        return RepeatStatus.FINISHED;
    };
}

Then, I've another Spring batch step with a MultiResourcesItemReader that read files in that directory :

@Bean
public MultiResourceItemReader multiResourceItemReader() throws IOException {

    ResourcePatternResolver resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
    Resource[] resources = resourcePatternResolver.getResources(securisationPaymentConfigProperties.getInputFiles());
    MultiResourceItemReader resourceItemReader = new MultiResourceItemReader();
    resourceItemReader.setResources(resources);
    resourceItemReader.setDelegate(xmlFileItemReaderBuilderJaxb2Marshaller());
    resourceItemReader.setSaveState(false);
    return resourceItemReader;
}

My issue is that when the reader try to open the files he found, he throws an exception :

Caused by: java.lang.IllegalStateException: Input resource must exist (reader is in 'strict' mode)

I tried to open the file with notepad++, the file is openable and utf-8 encoded.

Can you help me ? Thanks a lot.

question from:https://stackoverflow.com/questions/65943373/spring-batch-is-unable-to-read-a-xml-file-it-created

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
167 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...