-2

I made a Program which moves all files of a folder to another folder but when I'm executing the Program it shows me java.io.FileNotFound Exception and Access is denied

I tried to fix this error but it is still giving me the same error. What am I doing wrong? I have also attached the screenshot of Error I am getting.

when I checked the properties of the folder it was read-only, I changed but still, the Issue Persists

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class MoveFile
{
    public static void main(String\[\] args)
    {   

        InputStream inStream = null;
    OutputStream outStream = null;

        try{

            File afile =new File("C:\\Users\\admin\\Desktop\\A");
            File bfile =new File("C:\\Users\\admin\\Desktop\\B");

            inStream = new FileInputStream(afile);
            outStream = new FileOutputStream(bfile);

            byte\[\] buffer = new byte\[1024\];

            int length;

            while ((length = inStream.read(buffer)) > 0){

                outStream.write(buffer, 0, length);

            }

            inStream.close();
            outStream.close();


            afile.delete();

            System.out.println("File Moved!");

        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

I expect the Files of folder A to move in Folder B.

das

user207421
  • 305,947
  • 44
  • 307
  • 483
Abhi
  • 1
  • 4
  • 1
    I don't see any code that tries to move files in folder; I only see code that tries to copy a single file. You haven't shown the exact exception and at which line in your code it occurred. If you're trying to run this code on a folder, it will certainly fail because you cannot open a folder as an `FileInputStream`. – Erwin Bolwidt Jul 18 '19 at 04:04
  • @ErwinBolwidt It is giving me exception at File afile =new File("C:\\Users\\admin\\Desktop\\A"); line it shows java.io.FileNotFound Exception: C:\Users\admin\Desktop\A How can I open a folder?? – Abhi Jul 18 '19 at 04:17
  • are you sure the error is at the `new File` statement? the only checked Exception thrown by it is the `NullPointerException` - but if you are trying to create a `FileInputStream` on a directory, then you will get that exception (that would be the `new FileInputStream` line) - if you want help you should give correct information.... – user85421 Jul 18 '19 at 04:39
  • @CarlosHeuberger I have written the code to Copy files of a folder to another folder then after completion it will delete the files from the source folder... It is giving me error java.io.FileNotFound Exception: C:\\Users\\admin\\Desktop\\A at java.io.InputStream.Open0 at java.io.InputStream.Open at java.io.InputStream.init at MoveFile.main – Abhi Jul 18 '19 at 04:47
  • No, it is not giving you that exception at `new File(...)`, it is giving you the exception at `new FileInputStream(...)` or `new FileOutputStream(...)` or both. In either case it is because you don't have permission to read or write the file respectively. Not a coding problem, and not described accurately. – user207421 Jul 18 '19 at 05:01
  • and line 23 is `File afile = ...` ? or is it more the line with `... new FileInputStream`? maybe some other question can give inspiration: https://stackoverflow.com/q/12563955/85421 – user85421 Jul 18 '19 at 05:02
  • @CarlosHeuberger Thanks, that code is working but it is moving Folder A into Folder B...I want folder A to get empty and move all files to Folder B – Abhi Jul 18 '19 at 06:24
  • "inspiration" meant as an idea that you can adapt to your needs – user85421 Jul 18 '19 at 06:57
  • @CarlosHeuberger I have attached the screenshot of the error which I was getting at my code...have a look at it, please... – Abhi Jul 18 '19 at 07:09
  • well, strange enough the error was at line 23, now it is 21 , it was also on a method that does not throw such exception... very hard to know what really happened (BTW last line of that trace gives something like `MoveFile.java:23` which gives the exact location where the error happened - but we have no way to know which is line 23 (or 21)) – user85421 Jul 18 '19 at 09:18
  • @Abhi The stack trace you have posted in your comment bears little or no resemblance to the real stack trace posted as an image with your question. In either case it most certainly does *not* contain `File.`, which is effectively what you are contending here. – user207421 Jul 18 '19 at 10:10

1 Answers1

0

according to your question description , you try to move all file of a folder to another folder...
to do this you can use this code ....

public static void main(String[] args) throws IOException {
        String from = "C:/Users/Infra/Desktop/PBL/A";
        String to = "C:/Users/Infra/Desktop/PBL/B";
        File folder = new File(from);
        File[] listOfFiles = folder.listFiles();
        for (File file : listOfFiles) { 
            Path temp = Files.move(Paths.get(from +"/"+ file.getName()),
                    Paths.get(to +"/"+ file.getName()));  
        } 
    }
Istiaque Hossain
  • 2,157
  • 1
  • 17
  • 28