为了账号安全,请及时绑定邮箱和手机立即绑定

为什么分隔符会被 Path parentPath = Paths.get(strPath) 改变?

为什么分隔符会被 Path parentPath = Paths.get(strPath) 改变?

陪伴而非守候 2023-02-16 16:37:47
在我的 Windows 应用程序中,我想使用 FTP 删除服务器上的目录结构SimpleFileVisitor<Path>。它因“找不到文件”而失败,因为在下面的代码中,分隔符更改为反斜杠。显然,服务器希望它是正斜杠。我怎样才能让它保持正斜杠?public class FTPTest {static String server ;static int port ;static String user ;static String pass; static FTPClient theFtpClient;public FTPTest(){    server = "nx.dnslinks.net";    port = 21;    user = "xxxx";    pass = "#xxxxx";     theFtpClient = new FTPClient();}static void deleteDirectoryWalkTree(Path path) throws IOException {FileVisitor visitor = new SimpleFileVisitor<Path>() {    @Override    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {      Files.delete(file);      return FileVisitResult.CONTINUE;    }    @Override    public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {      Files.delete(file);      return FileVisitResult.CONTINUE;    }    @Override    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {      if (exc != null) {        throw exc;      }      Files.delete(dir);      return FileVisitResult.CONTINUE;    }};    Files.walkFileTree(path, visitor);}    public static void main(String[] args) {    FTPTest     theFTPTest = new FTPTest();    Path Path = Paths.get("/httpdocs/manual-uploads/TestingFTPUtil/SubDir_1/SubDir_2");    try {        theFTPTest.deleteDirectoryWalkTree(Path);    } catch (IOException ex) {        Logger.getLogger(FTPTest.class.getName()).log(Level.SEVERE, null, ex);    }}}
查看完整描述

1 回答

?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

PathPathsclasses 用于将您的代码与本地操作系统的路径语法隔离开来。

虽然您想使用远程FTP 系统的路径,但它可能(确实)使用不同的语法。主要是相同的语法,无论您的代码恰好在哪个本地操作系统上运行。

所以你不应该使用PathPathsclasses 作为 FTP 路径。


查看完整回答
反对 回复 2023-02-16
  • 1 回答
  • 0 关注
  • 189 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号