Program includes extension in name when moving files

2

I have a program in Java where when copying a group of files, when copying them, I copy them in the following way, this is the code of the method where you copy them:

public static void copyFiles(String src, String dst)
{
    int x = 0;
    eFilex = new File(src);
    listOfFiles = eFilex.listFiles();
    for (int i = 0; i < listOfFiles.length; i++)
        {
            if (listOfFiles[i].isFile()) 
            {
                fileName = listOfFiles[i].getName();//Aqui es donde obtiene el nombre
                if (fileName.endsWith(".pdf") || fileName.endsWith(".PDF"))//Evalua que sea tipo PDF
                {
                    try {
                            srcPth = Paths.get(src, fileName); 
                            eNumber = getNumber(fileName);
                            fDirectory = NewFolderNumber(dst, eNumber);
                            dstPth = Paths.get(fDirectory, fileName);
                            eFilex = new File(fDirectory, fileName);
                            if(!eFilex.exists())
                            {
                                try
                                {
                                    Files.copy(srcPth, dstPth, StandardCopyOption.COPY_ATTRIBUTES);
                                    System.out.println("Archivo organizado");
                                    x++;
                                }
                                catch (Exception e) 
                                {
                                   System.out.println(e.getMessage());
                                }
                            }
                            else
                            {
                                System.out.println("Ya existe el archivo..");
                            }
                        } 
                        catch (Exception e) 
                        {
                            System.out.println(e.getMessage());
                        }
                }
            }
        }
    System.out.println("Archivos copiados: " + x);
}

In the variables that the method receives, there are only two addresses that I have in a propertie. The problem is that when you copy them in the new folder, put the name with the extension, to be called "S00A-205420-0000", now it is called "S00A-205420-0000.pdf" What is causing that?

    
asked by Kevin M. 25.02.2016 в 18:16
source

2 answers

2

Your code what it does is find the files whose name you have when you finish ".pdf":

 if (fileName.endsWith(".pdf") || fileName.endsWith(".PDF"))//Evalua que sea tipo PDF

based on this you are copying the files with .pdf extension!

By the way you can simplify the above with:

if (fileName.toLowerCase().endsWith(".pdf"))//Evalua que sea tipo PDF

Do you want to detect files that end with .pdf and copy them without an extension?

You can use this method to remove the extension of names:

 static String eliminaExtension (String str) {
        if (str == null) return null;
        int pos = str.lastIndexOf(".");
        if (pos == -1) return str;
        return str.substring(0, pos);
    }

For example, the above mentioned method you would use it on the line:

Files.copy(srcPth, eliminaExtension(dstPth), StandardCopyOption.COPY_ATTRIBUTES);
    
answered by 25.02.2016 / 19:41
source
0

Look at this method you can use JAVA 7 up you could put validations to the destination so you do not overwrite it but it will work without affecting the extension.

 public synchronized static void copyFile(String origen, String destino) throws IOException {
       // System.out.println(origen);
      //  System.out.println(destino);
        Path FROM = Paths.get(origen);
        Path TO = Paths.get(destino);
        //sobreescribir el fichero de destino, si existe, y copiar
        // los atributos, incluyendo los permisos rwx
        CopyOption[] options = new CopyOption[]{
            StandardCopyOption.REPLACE_EXISTING,
            StandardCopyOption.COPY_ATTRIBUTES
        };
        Files.copy(FROM, TO, options);



    }
    
answered by 08.03.2016 в 20:27