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();
        chooser.showOpenDialog(null);
        File f = chooser.getSelectedFile();
        filename = f.getAbsolutePath();

       file_picker1.setText(filename);


       try{

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

          ByteArrayOutputStream  bos = new ByteArrayOutputStream();

           byte[] buf = new byte[1024];

           for (int readNum; (readNum=fis.read(buf))!=-1; ){

               bos.write(buf,0,readNum);

           }
            bill_image = bos.toByteArray();



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

this is the code to enter pics in SQLite

 try{

                 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);

                Inv.setText(nrt3);

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

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

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



            }finally{
            try{
             rs.close();
             pst.close();                
            }
              catch(Exception e){        
                                }    

            }

this is to move the file

String location = BatchDataManager.tke1.getText();

     try{

          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

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

how can I do that?

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

fis.close(); etc, of course

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.