I've created an algorithm to go through a directory tree and mirror its contents. The exception occurs when the program enters a directory with 400 images. It makes the copies of the images until it reaches an image around 320 and throws the following exception, in the method copyFile, conditional isImageJPG:
Exception in thread "main" java.lang.IllegalArgumentException: image == null!
at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(Unknown Source)
at javax.imageio.ImageIO.getWriter(Unknown Source)
at javax.imageio.ImageIO.write(Unknown Source)
at Main.copiaArquivo(Main.java:53)
at Main.entraDiretorio(Main.java:44)
at Main.entraDiretorio(Main.java:42)
at Main.entraDiretorio(Main.java:42)
at Main.entraDiretorio(Main.java:42)
at Main.main(Main.java:31)
The code is as follows:
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import javax.imageio.ImageIO;
public class Main {
private static File raizDiretorio;
private static File raizDiretorioCP;
private static String caminho = "D:\Ultimate Collection\info\xyz\";
public static void main(String[] args) throws IOException {
raizDiretorio = new File(caminho);
raizDiretorioCP = new File(caminho + "copia\");
raizDiretorioCP.mkdir();
File[] files = raizDiretorio.listFiles();
entraDiretorio(files);
}
private static void entraDiretorio(File[] files) throws FileNotFoundException, IOException {
for (File file : files) {
if (file.isDirectory() && !file.getAbsolutePath().equals(raizDiretorioCP.getAbsolutePath())) {
File[] listSubDiretorio = file.listFiles();
file = new File(preparaCaminhoFileSaida(file));
System.out.println("Criando diretorio " + file.getAbsolutePath());
file.mkdir();
entraDiretorio(listSubDiretorio);
} else if (file.isFile() && !file.getAbsolutePath().equals(raizDiretorioCP.getAbsolutePath())) {
copiaArquivo(file);
}
}
}
private static void copiaArquivo(File file) throws FileNotFoundException, IOException {
String caminhoSaida = preparaCaminhoFileSaida(file);
if (isImagemJPG(file)) {
BufferedImage img = ImageIO.read(file);
System.out.println("Copiando imagem de :" + file.getPath() + "\n para: " + caminhoSaida);
ImageIO.write(img, "jpg", new File(caminhoSaida));
} else
{
File outFile = new File(caminhoSaida);
if (!outFile.exists() && outFile.isDirectory())
outFile.mkdir();
System.out.println("Copiando arquivo: " + file.getAbsolutePath());
OutputStream ou = new FileOutputStream(outFile);
OutputStreamWriter osw = new OutputStreamWriter(ou);
BufferedWriter bw = new BufferedWriter(osw);
String texto = "";
BufferedReader reader = new BufferedReader(new FileReader(file));
String linha = reader.readLine();
while (linha != null) {
texto += linha;
linha = reader.readLine();
}
texto = texto.replaceAll("nomealeatorio", "");
bw.write(texto);
bw.close();
}
}
private static boolean isImagemJPG(File file) {
return file.getName().endsWith(".jpg");
}
private static String preparaCaminhoFileSaida(File file) throws IOException {
String path = file.getAbsolutePath();
path = path.replace(caminho, raizDiretorioCP.getCanonicalPath() + "\");
return path;
}
}
If I put a conditional to skip this photo that throws the exception the program continues but after some photos it re-launches the exception in another photo. I already checked the extension of the photos and they are all jpg. There is no other type of file in the directory. I'm not understanding why the algorithm works to some extent and then not.