简体   繁体   中英

Java: Passing file name and string to the command line

I have a small Java program where I'm trying to pass a text file and one or more strings separated by white spaces at the command line. Inside the text file, I have a list of packages as follows:

gui -> awtui swingui swingui -> runner extensions textui -> runner
framework awtui -> runner runner -> framework extensions -> framework

Now in my java, I want to list each package and its dependencies by passing the text file and package ame at the commandline as follows:

$ java PackageList sample.txt gui swingui

When I press enter, I would like to list each package as follows at the console:

gui -> awtui swingui
swingui -> runner extensions

As you can see, the rest of packages should not appear in the output if they are not passed at the command line as arguments.

Here's my Java code which wish is currently printing all the contents of the file regardless of the command line arguments after file name:

public class PackageList {

    public static void main(String[] args) {

        File inputFile = null;
        if (args.length > 0) {
            inputFile = new File(args[0]);
        } else {
            System.err.println("Invalid arguments count:" + args.length);
            System.exit(1);
        }

        BufferedReader br = null;

        try {
            String sCurrentLine;
            br = new BufferedReader(new FileReader(inputFile));
            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)
                    br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

You are reading only args[0] from the command line. You want to read all the other args like this.

After the file has been loaded add

for(int i=1; i<args.length;i++)
{
// read args[i]
}

Check below code and comments:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class PackageList {

    public static void main(String[] args) {

        File inputFile = null;
        if (args.length > 0) {
            inputFile = new File(args[0]);
        } else {
            System.err.println("Invalid arguments count:" + args.length);
            System.exit(1);
        }

        BufferedReader br = null;

        try {
            String sCurrentLine;
            br = new BufferedReader(new FileReader(inputFile));
            for(int i=1; i < args.length;i++){  // For loop to  check - args[0] is file name so i = 1 
                boolean isDependencyExist = false; // check dependency
                while ((sCurrentLine = br.readLine()) != null) {
                    if(sCurrentLine.startsWith(args[i])){ // If file line starts with arg print
                        System.out.println(sCurrentLine);
                        isDependencyExist = true;
                        break;
                    }
                }
            if(!isDependencyExist){
                System.out.println(args[i] + " ->");
            }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)
                    br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

It better to parse the file content by using Regex or StringTokenizer and put the keys and values into Map.

Then you can lookup the value by the commandline args and print it out.

Pseudocode

Map<String, String> deps = new Hashtable.....;
foreach line of file {
    String[] tokens = line.split(" -> ");
    deps.put(tokens[0], tokens[1]);
}
for (int i=1; i < args.size(); i++) {
    System.out.println(args[i] + " -> " + deps.get(args[i]));
}

Something like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM