简体   繁体   English

Java:将文件名和字符串传递给命令行

[英]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. 我有一个小的Java程序,我试图在命令行传递一个文本文件和一个或多个由空格分隔的字符串。 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中,我想通过在命令行传递文本文件和包ame来列出每个包及其依赖项,如下所示:

$ 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: 这是我的Java代码,它希望当前打印文件的所有内容,而不管文件名后的命令行参数如何:

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. 您只从命令行中读取args [0]。 You want to read all the other args like this. 你想读这样的所有其他args。

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. 最好使用Regex或StringTokenizer解析文件内容,并将键和值放入Map中。

Then you can lookup the value by the commandline args and print it out. 然后,您可以通过命令行args查找值并将其打印出来。

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. 这样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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