为什么在获取服务器目录时file.isDirectory()就不往下走了,直接就走完了,但是我访问
本地的却可以,求大神解答 public String readfile(String filepath, String hostFlow,String bizFlowNo){ File file = new File(filepath); if (!file.isDirectory()) { } else if (file.isDirectory()) { String[] filelist = file.list(); for (int i = 0; i < filelist.length; i++) { File readfile = new File(filepath + "\\" + filelist[i]); if (!readfile.isDirectory()) { String name = readfile.getName(); String[] namepathch = name.split(UNDERLINE); if (namepathch.length > 4){ namepathch = Arrays.copyOfRange(namepathch, 0, 5); StringBuffer split = new StringBuffer(); for (String temp : namepathch) { split.append(temp); split.append(UNDERLINE); } String substring = split.substring(0, split.length() - 1); if (substring.equals(hostFlow) ){ return readfile.getAbsolutePath(); }else{ StringBuffer stringBuffer = new StringBuffer(); String[] pathName = hostFlow.split(UNDERLINE); Arrays.fill(pathName,2,3,bizFlowNo); for(String s : pathName){ stringBuffer.append(s); stringBuffer.append(UNDERLINE); } String serialNumbe = stringBuffer.substring(0, stringBuffer.length() - 1); if (substring.equals(serialNumbe)){ return readfile.getAbsolutePath(); } } } } } } return null; }
在JAVA里面不能这样写,isDirectory返回真表示是文件夹,但为假也不一定是文件,要用isFile判断为真。
你本地是windows,服务器是不是linux?
你确定你的文件夹下面有文件吗?你确定进了 file.isDirectory() 判断吗?你用这个代码试试呢
public static String readfile(String filepath){
File file = new File(filepath);
if (!file.isDirectory()) {
} else if (file.isDirectory()) {
System.out.println("directory");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
System.out.println(filelist[i]);
}
}
return null;
}
if (!file.isDirectory())
也不报错,就直接走完了,这是什么原因
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632