Path and file name FileDialog

0

I need to know how I can get the route and name of a selected file using a FileDialog . In other words, when selecting a file and doing click on the open button, save the path of the file in String and in another string the name of the selected file.

  FileDialog dialogoArchivo;
  dialogoArchivo = new FileDialog(this, "Lista de Archivos desde Frame",FileDialog.LOAD);
    
asked by Abner 29.04.2017 в 06:26
source

1 answer

1

To obtain the total path of the selected File, the directory is first accessed using the getDirectory () y el nombre del archivo con getFile () , do not forget to validate that a file has been selected to not obtain a Exception NullPointerException

FileDialog dialogoArchivo;
dialogoArchivo = new FileDialog(this, "Lista de Archivos desde Frame",FileDialog.LOAD);
dialogoArchivo.setVisible(true);
if(dialogoArchivo.getFile()!=null){ /* Validar que se haya Seleccionado un Archivo*/
   String directorio = dialogoArchivo.getDirectory();
   String nombreArchivo =dialogoArchivo.getFile(); 
   String rutatotal = directorio + nombreArchivo;
}
else
   System.out.println("No Seleccionó Archivo");

OR Create an object of the class File and send by parameter the value that returns getFile () for later access the getAbsolutePath () method that returns the route total of the Archive.

String ruta = new File(dialogoArchivo.getFile()).getAbsolutePath();
    
answered by 29.04.2017 / 06:40
source