| @ -1,11 +1,123 @@ | |||
| package listarFichero; | |||
| public class listarFichero { | |||
| public static void main(String[] args) { | |||
| System.out.println("Hola"); | |||
| System.out.println("hola"); | |||
| import java.io.File; | |||
| import java.util.Calendar; | |||
| import java.util.Date; | |||
| import java.util.GregorianCalendar; | |||
| import java.util.Objects; | |||
| public class listarFichero { | |||
| public static void main(String[] args) { | |||
| File path = new File(System.getProperty("user.dir")); | |||
| File path3 = new File("/home/minumichaelubuntu/Documents/gitHub/OOP"); | |||
| // File path2 = new File((".")); | |||
| // File windowsPath = new File(("..//")); | |||
| // System.out.println(printSize(path2)); | |||
| listarDirectorio(path); | |||
| // System.out.println(printSize(path3)); | |||
| // System.out.println(printDate(path3)); | |||
| // System.out.println(printOwnership(path3)); | |||
| } | |||
| public static void listarDirectorio(File file) { | |||
| if(file.isDirectory()){ | |||
| String[] files = file.list(); | |||
| file.lastModified(); | |||
| int indent = 5; | |||
| System.out.printf("%10s %5s %6s %7s \n\n ", "NAME" ,"OWNERSHIP","SIZE", "DATE"); | |||
| for (int i = 0; i < Objects.requireNonNull(files).length; i++) { | |||
| System.out.printf("%10s %2s %5s %6s %2s %7s %n", files[i] , "", printOwnership(file), printSize(file),"", printDate(file)); | |||
| } | |||
| } | |||
| } | |||
| //Sacar el tamnyo del fichero | |||
| public static String printSize(File file) { | |||
| String resultado = ""; | |||
| long bytes = file.length(); | |||
| long kilobytes = (bytes / 1024); | |||
| long megabytes = (kilobytes / 1024); | |||
| long gigabytes = (megabytes / 1024); | |||
| long terabytes = (gigabytes / 1024); | |||
| long petabytes = (terabytes / 1024); | |||
| long exabytes = (petabytes / 1024); | |||
| long zettabytes = (exabytes / 1024); | |||
| long yottabytes = (zettabytes / 1024); | |||
| if(bytes > 0) { | |||
| resultado = bytes + "B"; | |||
| }if(kilobytes > 0) { | |||
| resultado = kilobytes + "KB"; | |||
| }if(megabytes > 0 ){ | |||
| resultado = megabytes + "MB"; | |||
| }if(gigabytes > 0 ) { | |||
| resultado = gigabytes + "GB"; | |||
| } | |||
| return resultado; | |||
| // return String.format("Size: %s", resultado); | |||
| // return String.format("%", null) | |||
| } | |||
| public static String printDate(File file){ | |||
| long ms = file.lastModified(); | |||
| Date d = new Date(ms); | |||
| Calendar c = new GregorianCalendar(); | |||
| c.setTime(d); | |||
| String dia, mes, annio, hora, minuto, segundo; | |||
| dia = Integer.toString(c.get(Calendar.DATE)); | |||
| mes = Integer.toString(c.get(Calendar.MONTH)); | |||
| annio = Integer.toString(c.get(Calendar.YEAR)); | |||
| hora = Integer.toString(c.get(Calendar.HOUR_OF_DAY)); | |||
| minuto = Integer.toString(c.get(Calendar.MINUTE)); | |||
| segundo = Integer.toString(c.get(Calendar.SECOND)); | |||
| return (hora + ":" + minuto + ":" + segundo + " " + dia + "/" + mes +"/" + annio); | |||
| } | |||
| public static String printOwnership(File file) { | |||
| String read = "", write = "", execute = ""; | |||
| if(file.canRead() == true) { | |||
| read = "r"; | |||
| }else {read = "r";} | |||
| if(file.canWrite()== true) { | |||
| write = "w"; | |||
| }else {write = "-";} | |||
| if(file.canExecute() == true) { | |||
| execute = "x"; | |||
| }else {execute = "-";} | |||
| return String.format("%s %s %s", read, write, execute); | |||
| } | |||
| } | |||