Directories and files in java

1

I have the following program:

  • If it's a file, I want you to show me (route, name, size)
  • if it's not a file show me then it's a directory.

the data takes it out of parameter.

The code:

public class VeureInfo {

      public static void VeureInfo(String filePath) throws FileNotFoundException, IOException {

            File origen = new File(filePath);
            Scanner reader = new Scanner(origen);   

            if (origen.isFile()){
             System.out.println("Java VeureInfo " +origen.getAbsolutePath());
             System.out.println("INFORMACIÓ: Informació sobre el fitxer:");
             System.out.println("Nom del fitxer : "+origen.getName());
             System.out.println("Ruta : "+origen.getPath());
             System.out.println("Ruta Absoluta : "+origen.getAbsolutePath());
             System.out.println("Es pot escriure : "+origen.canWrite());
             System.out.println("Es pot lleguir : "+origen.canRead());
             System.out.println("Grandaria : "+origen.length()+(" bytes"));

            }else{

               System.out.println("directorio") ;
            }

         }
          } catch (Exception e) {
     System.out.println("No hi han fitxers visibles que cumpleixin amb el patró :"+filePath);   
}



        }

public static void main(String args[]) throws IOException{
    VeureInfo.VeureInfo(args[0]);
}

}

I have a problem

Does my program read both things well, namely files and directories? I put a directory parameter and I miss error .. :( that is, it tells me that it does not find anything by the name of the directory .. What's wrong?

Thank you,

    
asked by Montse Mkd 20.09.2017 в 21:15
source

1 answer

2

Ok you have several errors in handling exceptions, in the static method with the name of the class, it should be the constructor of the class or not call the method as the class, so that it was the constructor you have to remove "static void "from the header of the method, or as I say, call the function with another name. Then you're throwing (throws statement) exceptions in the VeureInfo method but then you have half a statement (try-catch, it's a statement is not separated from one another!) Catch to capture an exception. If you throw it in the method, do not try to capture it too, or you close the door or leave it free xD. Then in the main method main () you should not re-throw the exceptions that can come out (throws) you have to capture them, here is the solution:

import java.io. *;

import java.util. *;

public class VeureInfo {

public static void veureinfo(String filePath) throws FileNotFoundException{

      File origen = new File(filePath);


      if (origen.isFile()){
       System.out.println("Java VeureInfo " +origen.getAbsolutePath());
       System.out.println("INFORMACIÓ: Informació sobre el fitxer:");
       System.out.println("Nom del fitxer : "+origen.getName());
       System.out.println("Ruta : "+origen.getPath());
       System.out.println("Ruta Absoluta : "+origen.getAbsolutePath());
       System.out.println("Es pot escriure : "+origen.canWrite());
       System.out.println("Es pot lleguir : "+origen.canRead());
       System.out.println("Grandaria : "+origen.length()+(" bytes"));

      }else{

         System.out.println("directorio") ;
      }

   }


public static void main(String args[]) {


    try {
        veureinfo(args[0]);
    } catch (FileNotFoundException error) {
        System.out.println(error);
    }
}

}

    
answered by 20.09.2017 / 22:45
source