[英]List of file and directory in directory in MAC OSX and Linux with Java
hello guys i have this code: 大家好,我有这段代码:
for (i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
if(!listOfFiles[i].isHidden()) {
files = listOfFiles[i].getName();
List=List+files+"?:";
}
}
else if(listOfFiles[i].isDirectory()) {
File Test = new File(listOfFiles[i]+"\\");
File [] Rec= Test.listFiles();
if(Rec!=null)
if(!listOfFiles[i].isHidden()) {
files=listOfFiles[i].getName();
List=List+files+"*:";
}
}
}
this code in windows return the real directory and all file in folder' but on mac and linux i see only the files.. why? Windows中的此代码返回真实目录和文件夹中的所有文件,但在Mac和Linux上,我仅看到文件..为什么?
You need to replace "\\\\"
with File.separator
. 您需要用
File.separator
替换"\\\\"
。
File Test = new File(listOfFiles[i]+File.separator);
This will then work in a cross-platform way: it'll use \\
on Windows, but /
on Mac and Linux. 然后,它将以跨平台的方式起作用:它将在Windows上使用
\\
,在Mac和Linux上使用/
。
One note of caution: there's also a File.pathSeparator
, and that has a tempting name for what you're doing, but it's not the right thing. 需要注意的一点是:还有一个
File.pathSeparator
, File.pathSeparator
您正在做的事情有一个吸引人的名称,但这不是正确的选择。 It's used for separating lists of paths, and it's ;
它用于分隔路径列表,它是
;
on Windows and :
on Mac and Linux. 在Windows上
:
在Mac和Linux上。
By the way, it's really not considered a good idea to have instance variables that start with a capital letter (like Test
above). 顺便说一句,以大写字母开头的实例变量确实不是一个好主意(例如上面的
Test
)。 Class names should be capitalized, but not method names or field names. 类名应大写,但不能使用方法名或字段名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.