我正在创建一个复制现有路径的方法'newOutputPath()',但它会更改提供的路径中的名称。例如,如果我有一个路径c:/../ parentDir / img.ext,此方法复制相同的路径,但将文件的名称更改为: C:/../ parentDir / AutoGen_img.ext
但是当我打电话时
System.out.println(FilePathUtils.newOutputPath());
它返回'请注意\ character:
C:/private/ArbeitsOrdner_19_Mar_2015/Images/FeaturesDetection/samples_00\AutoGenPath_queryjpg
正如你在我使用File.separator的代码中看到的,为什么它会导致这个错误的分隔符'\'我期待'/'呢?
码:
// i always add the first object but it's not must, we can choose any valid index or choose any valid index randomly.
FilePathUtils.outputFilePathToParentDirList.add(FilePathUtils.getInputFilePathToParentDirList().get(0));
Log.D(TAG, "newOutputPath", FilePathUtils.outputFilePathToParentDirList.get(0));
FilePathUtils.outputFilePathExtList.add(FilePathUtils.getInputFilePathExtList().get(0));
Log.D(TAG, "newOutputPath", FilePathUtils.outputFilePathExtList.get(0)); // prints C:/private/ArbeitsOrdner_19_Mar_2015/Images/FeaturesDetection/samples_00
FilePathUtils.outputImageNameList.add(SystemConstants.AUTO_GEN_PREFIX + FilePathUtils.getInputImageNameList().get(0));
Log.D(TAG, "newOutputPath", FilePathUtils.outputImageNameList.get(0)); //prints jpg
FilePathUtils.outputFileFullPathList.add(FilePathUtils.outputFilePathToParentDirList.get(0) + File.separator +
FilePathUtils.outputImageNameList.get(0) + FilePathUtils.outputFilePathExtList.get(0)); // prints AutoGenPath_query
if (FilePathUtils.getOutpathFileFullPathListSize() != -1) {
if (FilePathUtils.getOutpathFileFullPathListSize() == 0)
return FilePathUtils.outputFileFullPathList.get(0);
return FilePathUtils.outputFileFullPathList.get(FilePathUtils.getOutpathFileFullPathListSize() -1 );
} else {
Log.WTF(TAG, "newOutputPath", "outputFileFullPathList is null 'un-initialised at this far of the checking'");
return null;
}
/**
* This method is to return the size of the outputFileFullPathList list.
* @return
* the size of the outputFileFullPathList, or -1 if the outputFileFullPathList list is null "un-initialised" or 0 if it is empty.
*/
public static int getOutpathFileFullPathListSize() {
if (FilePathUtils.outputFileFullPathList != null) {
if (!FilePathUtils.outputFileFullPathList.isEmpty()) {
return FilePathUtils.outputFileFullPathList.size();
} else {
Log.D(TAG, "getOutpathFileFullPathListSize", "outputFileFullPathList list is empty");
return 0;
}
} else {
Log.E(TAG, "getOutpathFileFullPathListSize", "outputFileFullPathList list is null because it was not initilised, call StorePath method first to initlise it.");
return -1;
}
}
输出:
Debug: FilePathUtils -> newOutputPath: C:/private/ArbeitsOrdner_19_Mar_2015/Images/FeaturesDetection/samples_00
Debug: FilePathUtils -> newOutputPath: jpg
Debug: FilePathUtils -> newOutputPath: AutoGenPath_query
答案 0 :(得分:3)
因为Windows 上的文件分隔符是 \
而不是/
。 Windows在许多情况下都会接受,但官方的分隔符是反斜杠,因此Windows上的Java用于File.separator
。