2

I am trying to copy a file in my Class path to another temp location.

Here is the code for it:

    InputStream inputStream = this.getClass().getClassLoader()
            .getResourceAsStream(readmeFile);

    Path path = Paths.get(tempFilesOutputPath + File.separator + readmeFile);
    try {
        Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING);
        inputStream.close();

    } catch (IOException e) {
        throw e;
    }

readMeFile has 2 pages, the copied file in the tempFilesOutputPath folder also has two pages but without any content.

Please let me know if I am making some mistake or it has to be done in a different way.

Cheers, Madhu

Madhu V Rao
  • 917
  • 1
  • 12
  • 24
  • 1
    Are you sure your "InputStream" is being created correctly? Try "this.getClass().getResourceAsStream()" and see if it helps. – DeathByTensors Dec 17 '13 at 00:31
  • InputStream has been created properly, coz if I send a .txt file then it copies properly. – Madhu V Rao Dec 17 '13 at 01:05
  • 1
    Bizarre... My next step would be to look at the binary content of the created file to see what is and what isn't getting copied. It might help you understand the problem better. For example, if it is just the "top" of the file then the output buffer might not be properly flushed. – DeathByTensors Dec 17 '13 at 01:15

2 Answers2

9

Issue was totally unrelated. I was using maven copy resource to copy the resources under my src/main/resources/

this was my maven resource:

        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
                <include>**/*.txt</include>
                <include>**/*.html</include>
                <include>**/*.pdf</include>
            </includes>
        </resource>

Since the filtering was on PDF file was copied as an empty doco to the target folder.

I just seperated it into two resources with filtering off for PDF file.

        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
                <include>**/*.txt</include>
                <include>**/*.html</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <includes>
                <include>**/*.pdf</include>
            </includes>
        </resource>

Thanks to Drew Buckley, I got the issue when trying to do a binary comparison of the file. Actual file on the project was different and the one on the target folder which gets copied from the maven was different.

It works fine now.

Madhu V Rao
  • 917
  • 1
  • 12
  • 24
4

Yes, this worked for me too; in Maven documentation i found this for more information;

Warning: Do not filter files with binary content like images! This will most likely result in corrupt output. If you have both text files and binary files as resources, you need to declare two mutually exclusive resource sets. The first resource set defines the files to be filtered and the other resource set defines the files to copy unaltered

Andrea Cip
  • 41
  • 4
  • Im not sure this really answer the question, and is probably better left as a comment (when you have enough rep) – Stuart Siegler Apr 13 '15 at 16:05
  • 1
    i cant with my rep, so i would like to share what i have found in maven documentation to explain better the cause of the problem – Andrea Cip Apr 13 '15 at 17:28
  • I understand, and I/we appreciate that. First answers are reviewed, and we're supposed to help and encourage new users; at the same time, we're being reviewed, hence the note. – Stuart Siegler Apr 13 '15 at 17:36