Args [0] by console - JAVA

-1

I have the following program:

public static void veureinfo(String filePath) throws FileNotFoundException{
      // creem l'objecte
      File origen = new File(filePath);

      /**
       * Si es directori llavors:
       */
      if (origen.isFile()){

Basically the program takes me the argument of the program and uses it.

But, what if I want to give the argument as a console? That is, can it be ordered with the class Scanner ? The idea is that my program is like the Windows CMD ... so I need the user to decide which argument he wants to look for and my program finds it.

How is it done?

File ruta = new File ("C/");
File f = new File (ruta, ?????);

How do I get Scanner to create a file for me?

Scanner lector = new Scanner (System.in);
??? = lector.next?????
    
asked by Montse Mkd 24.09.2017 в 21:11
source

3 answers

1

I understand that you intend to request the file from the user without the need to attach it as an argument when executing the program.

<code>java nombre_programa</code>

With the Scanner class you can request the name of the file:

Scanner sc = new Scanner(System.in);

System.out.print("Nombre del fichero: ")
String nombre_fichero = sc.nextLine();

File file = new File(nombre_fichero);

Now you must bear in mind that you are using a particular constructor of the File object, so you have the following possibilities:

Suppose the file is "data_of_test.txt".

  • We can create the File object associated with this file in the same working directory:

    File file = new File("datos_de_prueba.txt");

  • Creation of the File object associated with this file, within a file of the same working directory: (This is what is known as relative path)

    File file = new File("datos/datos_de_prueba.txt");

  • The File object associated with this file is created using the absolute path:

    File file = new File("c:/Users/ ... Ruta hasta el directorio de trabajo ... /datos/datos_de_prueba.txt");

  • If we omit the letter of the unit, the letter is taken by default where the project is.

        
    answered by 25.09.2017 / 00:32
    source
    1
    public static void main(String ...args){
    
        veureinfo(args[0]);
    
    }
    

    This way you would have to run the program from the cmd so:

    javac nombrearchivo.java
    java nombrearchivo "ruta del fichero"
    

    If instead you just want the user to be able to enter the route from the cmd when the program is running, then it would be:

    public static void main(String...args){
    
      System.out.println("Introduzca la ruta del archivo");
      String ruta="";
      Scanner teclado=new Scanner(System.in);
      ruta=teclado.nextLine();
      NombreClase.veureinfo(ruta);
    
    }
    

    You speak in the title of args [0] and in the question of scanner they are totally different things, args [] is an array of arguments type string that can receive a java program when being executed from console, adding these arguments of the the way I explained above and separated by commas each argument will be an element of the args [] array. On the other hand, the Scanner class is used so that in the execution of a program the user is allowed to enter any data by means of the keyboard at the moment that you use their methods.

        
    answered by 24.09.2017 в 23:46
    -3

    PHP has a special variable that you can use to access the arguments if the script is running on the terminal.

    Access to it; $argv .

        
    answered by 24.09.2017 в 21:59