the process cannot access the file because it is being used by another process java

I use this code to transfer a file to an other

 Files.move(Paths.get(file_picker1.getText()),  Paths.get("random location\\.png"));

I understand that I have to close it. but Im not finidng how to do that. search in google but no result about this yet.

Maybe you have an earlier version of the code that started executing but has not terminated for some reason? Check your task manager for running versions of java.exe or javaw.exe

If you are just using Files.move then there's no need to close anything. You only need to close input/output streams if you use those.

Here is all the code that is in the file

this is the code which execute before. I get the pic:

 JFileChooser chooser = new JFileChooser();
        File f = chooser.getSelectedFile();
        filename = f.getAbsolutePath();



           File image = new File (filename);
           FileInputStream fis = new FileInputStream(filename);

          ByteArrayOutputStream  bos = new ByteArrayOutputStream();

           byte[] buf = new byte[1024];

           for (int readNum; (!=-1; ){


            bill_image = bos.toByteArray();

       catch(Exception e){
       JOptionPane.showMessageDialog(null, e);

this is the code to enter pics in SQLite


                 int nrt = Integer.parseInt(Inv.getText());
                 int nri = Integer.parseInt(LBL2.getText());
                 int nrtf = nrt+1;

                 String nrt1 = Integer.toString(nri); 
                 String nrt2 = LBL2.getText() +"_"+ nrtf;
                String nrt3 = Integer.toString(nrtf);


                String sql= "INSERT INTO Pics (Invoicenumber, Photo, PhotoID) VALUES (?,?,?)";
                pst.setString(1, LBL2.getText());
                pst.setBytes(2, bill_image);
                pst.setString(3, nrt2);

                JOptionPane.showMessageDialog(null, "Pic Imported");

            }catch (Exception e){
                JOptionPane.showMessageDialog(null, e);

              catch(Exception e){        


this is to move the file

String location = BatchDataManager.tke1.getText();


          Files.move(Paths.get(file_picker1.getText()),  Paths.get(location +"_"+Inv.getText()+".png"));

        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);

which do I have to close at this? because Im not understanding

Edited 1 Year Ago by altjen

Also, even if it show that error that is being used by an other. it upload the pic on SQLite

this confuse me a lot

Edited 1 Year Ago by altjen

I cant find the right words to thank you enough sir!

In that first piece of code you definitely need to close the streams.

Would flush have worked in this case?

I doubt it. flush() will ensure that any buffered data Java has in memory will be written to the stream, but it will leave the stream open, so the file will still be in use.

ps: re flush() Note this warning from the JavaDoc:
If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

Edited 1 Year Ago by JamesCherrill

This question has already been answered. Start a new discussion instead.