作者:手机用户2502917943 | 来源:互联网 | 2023-10-17 18:12
Ihavebeenworkingwithbufferingafileonmylocaldrivetoparseandobtaincertaindata.Forte
I have been working with buffering a file on my local drive to parse and obtain certain data. For test purposes I was easily able to do it this way:
我一直在缓冲本地驱动器上的文件来解析并获取某些数据。出于测试目的,我很容易就能这样做:
public static void main(String[] args) {
fileReader fr = new fileReader();
getList lists = new getList();
File CP_file = new File("C:/Users/XYZ/workspace/Customer_Product_info.txt");
int count = fr.fileSizeInLines(CP_file);
System.out.println("Total number of lines in the file are: "+count);
List lines = fr.strReader(CP_file);
....
}
}
fileReader.java file has the following function:
fileReader.java文件具有以下功能:
public List strReader (File in)
{
List totLines = new ArrayList();
try
{
BufferedReader br = new BufferedReader(new FileReader(in));
String line;
while ((line = br.readLine()) != null)
{
totLines.add(line);
}
br.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//String result = null;
return totLines;
}
Now I want the file path to be passed as a Command line Argument instead. I tried a few things but I am kind of new to this and wasnt able to make it work. Can someone please help and explain what all changes I need to make in order to incorporate that change in my code.
现在我希望文件路径作为命令行参数传递。我尝试了一些东西,但我对此并不熟悉并且无法使其正常工作。有人可以帮助解释我需要做出的所有更改,以便将更改合并到我的代码中。
4 个解决方案
1
Your problem has two sides: how to pass the parameter from the command line, and how to read it inside your code.
您的问题有两个方面:如何从命令行传递参数,以及如何在代码中读取它。
Passing arguments to a Java program
All arguments meant for the actual Java class and not for the JVM, should be put after the class name, like this:
所有参数都是针对实际的Java类而不是针对JVM的,应该放在类名后面,如下所示:
C:\YOUR\WORKSPACE> java your.package.YouMainClass "C:\Users\XYZ\workspace\Customer_Product_info.txt"`
Things to watch out for:
需要注意的事项:
- Slashes
/
vs backslashes \
: since you're on a Windows system, I'd rather use backslashes for your path, specially if you are including the drive letter. Java can work with both variants, but it's better to follow your SO conventions.
斜杠/ vs反斜杠\:因为你在Windows系统上,我宁愿使用反斜杠作为你的路径,特别是如果你包括驱动器号。 Java可以使用这两种变体,但最好遵循您的SO约定。
- Double quotes
"
to allow for spaces: you'll need to enclose your path in double quotes if any directories contain spaces in their names, so just double quote it every time.
双引号“允许空格:如果任何目录的名称中包含空格,则需要将路径括在双引号中,因此每次都要双引号。
- Remove final backslash: this only applies to directory paths (file paths can't end in a backslash in Windows). If you write a directory path like this:
"C:\My path\XYZ\"
, the last double quote will be included as part of the path, because of the previous backslash escaping it \"
. Instead, "C:\My path\XYZ"
will do fine.
删除最后的反斜杠:这仅适用于目录路径(文件路径不能以Windows中的反斜杠结尾)。如果你编写一个这样的目录路径:“C:\ My path \ XYZ \”,最后的双引号将作为路径的一部分包含在内,因为前面的反斜杠转义它“。而是,”C:\ My path \ XYZ“会很好。
Reading arguments from your main(String[])
method
Now this one is simple: as others have pointed out, the String with your path should now be in args[0]
:
现在这个很简单:正如其他人指出的那样,带有路径的String现在应该在args [0]中:
public static void main(String[] args) {
fileReader fr = new fileReader();
getList lists = new getList();
if (args[0] == null || args[0].trim().isEmpty()) {
System.out.println("You need to specify a path!");
return;
} else {
File CP_file = new File(args[0]);
int count = fr.fileSizeInLines(CP_file);
System.out.println("Total number of lines in the file are: "+count);
List lines = fr.strReader(CP_file);
....
}
}
I added some null-checking to avoid problems like the ArrayIndexOutOfBounds
you run into.
我添加了一些空值检查,以避免遇到像您遇到的ArrayIndexOutOfBounds之类的问题。