遇到了理解这个测试案例代码在做什么的困难。

huangapple go评论62阅读模式
英文:

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();

huangapple
  • 本文由 发表于 2020年10月22日 03:18:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64470285.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定