很多时候,我发现自己创建了两个物体像这样:如何命名在Java中
String pathString = "/foo/bar";
File path = new File(pathString);
虽然变量命名是一个相当主观的问题,什么是最常见的命名约定File
对象及其对应的绝对文件路径存储在String
?
很多时候,我发现自己创建了两个物体像这样:如何命名在Java中
String pathString = "/foo/bar";
File path = new File(pathString);
虽然变量命名是一个相当主观的问题,什么是最常见的命名约定File
对象及其对应的绝对文件路径存储在String
?
我想这里没有命名约定。我会看看constructor argument of File并在此后命名,例如
String pathname = "/foo/bar";
File file = new File(pathname);
通常文件在您的应用程序中有意义。所以我会选择一个描述它的名字。例如。
String errorLogFilepath = "/var/log/error.log";
File errorLogFile = new File(errorLogFilepath);
您是否需要独立识别路径?
如果没有,身份证建议只是用:
File path = new File("/foo/bar");
同样,来传递文件对象,而不是字符串的路径。
如果不重用在代码中进一步引用了这条路,建议你通过传递一个arguement文件初始化。例如
File file = new File("/foo/bar");
这将防止创建额外的字符串变量。
我总是使用双反斜线,我从来没有有这个问题:
File outputfile = new File("c:\\Selenium\\workspace\\....\\input.txt");
我不认为有一个普遍接受的惯例,但某些项目可能有自己的。在这种特殊情况下,我会调用第一个变量'pathName'或完全清除它。 – biziclop 2014-10-06 11:01:19