0

I want to export the file in the package in my Java project. I use I / O for this. I can specify its location in InputStream as src / packagename / filename, it works like this, but it doesn't work when I make jar file.Also, I want to print out how much has been transferred from the code below, finally I want to show it in the progress bar. How can I do?

        inputStream = new FileInputStream(file1);
        outputStream = new FileOutputStream(file2);

        byte[] buffer = new byte[1024 * 4];

        int length;
        while ((length = inputStream.read(buffer)) > 0) {

        outputStream.write(buffer, 0, length);

        }


        
        file1.delete();
9AspecT
  • 11
  • 4

1 Answers1

0

Your are using a 'resource' file. These are files embeded in the jar. You have a special api to access them.

See Reading a resource file from within jar

Then output to console in the while loop to see how much is done. You can get the total file size before the loop. The write method return how much bytes was wrote. Accumulate this values and compute a % to print a progress bar

Tokazio
  • 516
  • 2
  • 18
  • Thank you. But my intention was not to read the file. I want to export the file in package A with these codes in class B. – 9AspecT Feb 19 '21 at 08:36
  • You want to put a file at runtime into the package (a folder in the jar) ?! – Tokazio Feb 19 '21 at 17:21