Home>

I want to create a program with command line arguments, but I don't know how to do it and an error occurs.

As an ideal execution result,

Alternate "☆☆☆☆☆" from test1.txt and "" from test2.txt on the screen.

☆☆☆☆☆

☆☆☆☆☆

☆☆☆☆☆

Is ideal, but by all means only the text of test1.txt is displayed... (Is it wrong that only test1.txt was specified in the execution configuration...)?

Specifically, I don't know how to name the file as a command line factor and how to display more than one file on the console.

For the pictures below, the first is the program that is the basis of the command line arguments.
The second is the test2.txt file
The third is the test1.txt file
is.

  • Answer # 1

    if (args.length != 1) {Toif (args.length != 2) {Did you change to?

    Now just repeat the same thing for args[0] and args[1].

              BufferedReader br;
                String str;
                for (int i = 0;i<3;i++) {
                    br = new BufferedReader(new FileReader(args[0]));
                    while ((str = br.readLine()) != null)
                        System.out.println(str);
                    br.close();
                    br = new BufferedReader(new FileReader(args[1]));
                    while ((str = br.readLine()) != null)
                        System.out.println(str);
                    br.close();
                }


    I'm not sure if it worked, but if I understood this code
    Please write in the comments.

  • Answer # 2

    The command line arguments received are separated by spaces and are in order in the args array.

    If you want to view the contents of two files, open the second file and view it as well.