首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么分隔符会被路径parentPath = Paths.get(strPath)更改?

为什么分隔符会被路径parentPath = Paths.get(strPath)更改?
EN

Stack Overflow用户
提问于 2019-06-24 17:36:26
回答 1查看 54关注 0票数 1

在我的Windows中,我想使用SimpleFileVisitor<Path>删除服务器上的目录结构。它与“文件未找到”一起失败,因为在下面的代码中,分隔符被更改为反斜杠。显然,服务器希望它是正斜杠。我怎么才能让它保持为正斜线呢?

代码语言:javascript
复制
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);
    }
}

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-24 18:12:07

Path/Paths类用于将代码与本地操作系统的路径语法隔离开来。

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

因此,您不应该对FTP路径使用Path/Paths类。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56741460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档