英文:
How to get File name from cmd argument to create a copy?
问题
我正在开发一个程序,该程序从我创建的自定义扩展名文件中读取内容。想法是每次读取文件时都会创建一个错误报告。错误报告必须位于调用源文件的文件夹中。错误文件是源文件的副本,但每行开头都有行号,并在行尾指示是否发生了错误。
(这里不涉及问题的编号设置,问题只是关于创建副本的)
例如,当我从命令提示符中调用我的程序时:
C:\MyLocation>java -jar myJavaProgram.jar myFileToRead.CustomExtension
除了读取文件之外,还应在相同位置创建一个名为 myFileToRead-ErrorReport.txt 的副本。
另外:如果源文件没有扩展名,我必须假设它仍然是正确的扩展名,因此并不总会有一个 '.myCustomExtension' 的部分需要替换为 .txt。
问题在于我不知道如何获取文件名,因为它来自于主方法的 args 列表。我正在使用以下代码来读取文件:
public static void main(String[] args) throws FileNotFoundException {
try {
File inputFile = new File(args[0]);
Scanner sc = new Scanner(inputFile);
while (sc.hasNext()) {
System.out.println(sc.nextLine());
}
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
System.out.println("File not found.");
}
}
那么,我如何获取文件名以创建类似以下代码的内容:
File errorReport = new File("./" + inputFileName + ".txt");
英文:
I'm working on a program that reads from a file with a custom extension I made. The idea is that an error report is created every time a file is read. The error report must be in whatever folder the source file was called from. The error file is a copy of the source file, but it has a line number at the beginning of each line and indicates at the end of the line if an error occurred at that line.
(I'm not trying to set up the numbering on this question, this question is just about creating the copy)
So for example, when I call my program from the command prompt:
C:\MyLocation>java =jar myJavaProgram.jar myFileToRead.CustomExtension
Asides from reading the file, it should also create a copy at the same location called myFileToRead-ErrorReport.txt
Additionally: If the source file has no extension, I have to assume that it's still the correct extension, so there won't always be a '.myCustomExtension' segment to replace into .txt
The problem is that I don't know how to grab the file name, because it's coming from the args list of the main method. I am using the following to read the file
public static void main(String[] args) throws FileNotFoundException {
try{
File inputFile = new File(args[0]);
Scanner sc = new Scanner(inputFile);
while(sc.hasNext()){
System.out.println(sc.nextLine());
}
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
System.out.println("File not found.");
}
}
So how can I get that file name to make something like
File errorReport = new File("./" + inputFileName + ".txt");
?
答案1
得分: 1
以下是翻译好的代码部分:
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("错误:缺少文件名参数。");
}
else {
String filename = args[0];
if (filename.length() == 0) {
System.out.println("错误:空文件名参数。");
}
else {
if (!filename.endsWith(".CustomExtension")) {
filename += ".CustomExtension";
}
String name = filename.substring(0, filename.indexOf(".CustomExtension"));
name += "-ErrorReport.txt";
File inputFile = new File(filename);
File directory = inputFile.getParentFile();
File errorReport = new File(directory, name);
System.out.println(errorReport.getAbsolutePath());
}
}
}
如果您需要更多帮助,请随时提问。
英文:
First the code. The explanations appear after the code.
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("ERROR: Missing filename argument.");
}
else {
String filename = args[0];
if (filename.length() == 0) {
System.out.println("ERROR: Empty filename argument.");
}
else {
if (!filename.endsWith(".CustomExtension")) {
filename += ".CustomExtension";
}
String name = filename.substring(0, filename.indexOf(".CustomExtension"));
name += "-ErrorReport.txt";
File inputFile = new File(filename);
File directory = inputFile.getParentFile();
File errorReport = new File(directory, name);
System.out.println(errorReport.getAbsolutePath());
}
}
}
I make it a habit of checking the parameters. Hence I first check that the file name was supplied. If it was, then I check that it is not an empty string. Note that I have omitted some checks, for example checking whether the named file exists and is readable.
You wrote in your question that the file name argument may or may not include the .CustomExtension
. Hence I check whether the supplied name ends with the required extension and append it if necessary. Now, since I know what the file name ends with, that means that the required part of the name is everything up to the extension and that's what the call to substring()
gives me.
Once I have the required name, I just append the part that you want to append, i.e. -ErrorReport.txt
.
Method getParentFile()
in class java.io.File
returns the directory that the file is located in. Hence I have the directory that the input file is in. Finally I can create the error report file in the same directory as the input file and with the desired file name. For that I use the constructor of class java.io.File
that takes two parameters. Read the javadoc for details.
Note that creating a File
object does not create the file. Creating an object to write to the file does, for example FileWriter
or OutputStreamWriter
.
答案2
得分: 0
这是一个用于创建文件的代码示例,文件名作为命令行参数传递,并获取相同的文件名:
Class Demo{
public static void main(String[] args){
String path = "<文件路径>";
String name = args[0];
File f = new File(path + name + ".txt");
f.createNewFile(); //创建文件
System.out.println(f.getName()); //将会返回文件名
}
}
命令行输入:
java -cp . Demo.java <文件名>
注意:如果你的类文件位于当前目录中,命令中使用了'.'。
你可以参考这段代码并根据你的需求进行修改。希望这符合你的要求。
英文:
Here is the code example to create a file, with filename passed from cmd line as argument and to get the same file name :
Class Demo{
public static void main(String[]args){
String path ="<path of file>"
String name= args[0];
File f = new File(path+name+".txt");
f.createNewFile(); //create file
System.out.println(f.getName()); // will give you the file name
}
}
cmd line : java -cp . Demo.java <filename>
Note : '.' used in the cmd if your class file is present in current dir
You can refer the code and modify to suit your requirement.
Hope this is what you are looking for.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论