英文:
Having trouble understanding what this test case code is doing
问题
以下是您要求的翻译内容:
static String getBadPath(String name) {
return new File(new File(TestSuite.class.getResource("empty.txt").getPath()).getParent(), name).getAbsolutePath();
}
英文:
I am in CS1050, and we are doing a lab that includes grabbing information from files in order to print new information onto a different file. I have no idea what one of the methods my teacher wrote in the test case class is trying to do. Ive looked up all of the methods that this method uses, but I dont know what the end result is.
static String getBadPath(String name) {
return new File(new File(TestSuite.class.getResource("empty.txt").getPath()).getParent(), name).getAbsolutePath();
}
答案1
得分: 0
这基本上获取了一个名为 name
且位于与 empty.txt
相同目录中的文件的绝对路径。
你可以将其分解为以下代码:
// 获取名为 "empty.txt" 的 File 对象。
File emptyTxt = new File(TestSuite.class.getResource("empty.txt").getPath());
// 获取 emptyTxt 所在的目录。
File parentDirectory = emptyTxt.getParentFile();
// 获取与参数 name 相同名称且位于 parentDirectory 中的 File。
File resultFile = new File(parentDirectory, name);
// 返回 resultFile 的绝对路径。
return resultFile.getAbsolutePath();
请注意,我已将 HTML 编码的引号转换为正常引号。
英文:
This basically get the absolute path of a file whose name is name
and resides in the same directory with empty.txt
.
You can break it down into following code:
//get the File object named "empty.txt".
File emptyTxt=new File(TestSuite.class.getResource("empty.txt").getPath());
//get the directory this emptyTxt reside in
File parentDirectory=emptyTxt.getPath().getParent();
//get the File whose name is same as the parameter name and reside in parentDirectory.
File resultFile=new File(parentDirectory,name)
//return the absolute path of the resultFile
return resultFile.getAbsolutePath();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论