Java copy all JPEGs from a folder to another folder

191 views Asked by At

How do I copy all .jpgs from a folder over to another folder using FileInputStream?

At the moment I have it as this for copying my picture over to my SMB share:

    protected String doInBackground(String... params) {

        File file = new File("/sdcard/Data/pics/IMG.jpg");
        String filename = file.getName();
        NtlmPasswordAuthentication auth1 = new NtlmPasswordAuthentication(
                servername, username, password);

        try {

            SmbFile sfile = new SmbFile(servername + "/" + filename, auth1);
            if (!sfile.exists())
                sfile.createNewFile();
            sfile.connect();

            InputStream in = new FileInputStream(file);

            SmbFileOutputStream sfos = new SmbFileOutputStream(sfile);

            byte[] buf = new byte[8192];
            int len;
            while ((len = in.read(buf)) >= 0) {
                sfos.write(buf, 0, len);
            }
            in.close();
            sfos.close();

            z = "File copied successfully";
        } catch (Exception ex) {

            z = z + " " + ex.getMessage().toString();
        }

        return z;

    }
}

I had used lambdas before, like this:

File file = new File(("/sdcard/Data/pics);

File[] jpgFiles = file.listFiles((dir, name) -> name.endsWith(".jpg"));

for (File entry : jpgFiles) {
    System.out.println("File: " + entry.getName());
}

but since my program also should support older versions of Android I can't use it.

0

There are 0 answers