Advertisement
irenecg

Untitled

Oct 18th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package exercicisio;
  7.  
  8. import static exercicisio.Ex4.pedirTexto;
  9. import java.io.BufferedReader;
  10. import java.io.File;
  11. import java.io.IOException;
  12. import java.io.InputStreamReader;
  13. import java.nio.file.Files;
  14. import java.nio.file.Paths;
  15. import java.util.logging.Level;
  16. import java.util.logging.Logger;
  17.  
  18. /**
  19. *
  20. * @author THOR
  21. */
  22. public class Ex6 {
  23.  
  24. private static String directorio = "";
  25.  
  26. public static void main(String[] args) {
  27. boolean error = false;
  28. File file = null;
  29. do {
  30. directorio = pedirTexto("Introduce un directorio");
  31. // mostrarArchivos(directorio);
  32. file = new File(directorio);
  33. if (!file.isDirectory()) {
  34. error = true;
  35. } else {
  36. error = false;
  37. }
  38. } while (error);
  39. lista(directorio);
  40. }
  41.  
  42. public static void mostrarArchivos(String directoryName) {
  43. File directory = new File(directoryName);
  44. File[] fList = directory.listFiles();
  45. for (File fi : fList) {
  46. System.out.println(fi.getName());
  47. }
  48. }
  49.  
  50. public static void lista(String directoryName) {
  51. File directory = new File(directoryName);
  52. File[] fList = directory.listFiles();
  53. System.out.println("Se han creado: ");
  54. for (File fi : fList) {
  55. if (fi.isDirectory()) {
  56. File file = new File(directoryName + "\\" + fi.getName() + ".txt");
  57. try {
  58. file.createNewFile();
  59. } catch (IOException ex) {
  60. Logger.getLogger(Ex6.class.getName()).log(Level.SEVERE, null, ex);
  61. }
  62. System.out.println(file.getAbsolutePath());
  63. } else {
  64. String[] name = fi.getName().split("\\.");
  65. try {
  66. Files.createDirectories(Paths.get(directoryName + "\\" + name[0]));
  67. System.out.println(name[0]);
  68. } catch (IOException ex) {
  69. Logger.getLogger(Ex6.class.getName()).log(Level.SEVERE, null, ex);
  70. }
  71. }
  72. }
  73. }
  74.  
  75. public static int pedirEntero(String mensaje) {
  76. boolean error = true;
  77. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  78. int numero = 0;
  79. do {
  80. System.out.println(mensaje);
  81. try {
  82. numero = Integer.parseInt(br.readLine());
  83. error = false;
  84. } catch (IOException ex) {
  85. System.out.println("Error de entrada salida " + ex.getMessage());
  86. } catch (NumberFormatException ex) {
  87. System.out.println("Debes introducir un numero");
  88. }
  89. } while (error);
  90. return numero;
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement