Java args pass file as parameter

Asked

Viewed 1,773 times

2

I am doing a graph work where I have to make a library and after generating the jar in the execution I need to inform two parameters that are txt files, in the first will be the input data in the second output. My doubt is how do I pass two txts as args parameter ?

  • Pass the file path as parameter and instantiate as File in the application?

  • I did exactly that instead of trying to pass the files I pass their paths and it worked right.

  • I’ll put it in answer.

2 answers

1

Instead of trying to pass the files by parameter, pass the file path and instate with the class File in the application.

public static void main(string[] args){
    for(String arq : args){
        File file = new File(arq);
    }
}

0

public  void passarArquivos( String ... paths ) { 

        File arquivo ;

        for (String caminho : paths) {

            arquivo = new File( caminho );

            System.out.println(" Nome :"+ arquivo.getName() );

        }

 }


 public static void main(String[] args) {

        SuaClass suaClass = new SuaClass();

        suaClass.passarArquivo("C:\\arquivo1.txt" , "C:\\arquivo2.txt" );
 }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.