簡體   English   中英

Java:為什么要花幾秒鍾時間來檢查一個文件是否存在於不可訪問的 Windows 驅動器上?

[英]Java: why does it take several seconds to check if a file exists on an inaccessible Windows drive?

使用任何可用方法檢查現有文件通常非常快,但如果文件路徑包含不可訪問的 .network) 驅動器,則檢查需要幾秒鍾。

檢查不存在的驅動器(代碼示例中的“R:”)沒有問題,但檢查不再可訪問的已知驅動器(代碼示例中的“S:”)需要很長時間。

編輯:這在驗證最近文件列表(將每個無法訪問的文件乘以 2-4 秒)和打開標准 JFileChooser() 時都有顯着影響,這可能需要 1-2 分鍾,並且有幾個斷開連接的驅動器。

問題:

  1. 這是一個已知問題嗎?
  2. 有解決方法嗎?

上下文: Java 11 或 17 on Windows 10 具有現有的“C:”驅動器、不存在的“R:”驅動器和不可訪問的網絡驅動器“S:”。

示例代碼:

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;

public class FileSystemExperiment {

   private static final String[] FILES = {
      System.getProperty("user.home"), // home dir exists
      "/no/such/file",                 // missing file on current drive
      "C:\\no\\such\\file",            // missing file on existing drive
      "R:\\",                          // an unknown drive
      "S:\\",                          // a known but inaccessible drive
   };

   private static long started;

   private static void exists(String... pathStrings) {
      for (String pathString : pathStrings) {
         File file = new File(pathString);
         Path path = file.toPath();
         System.out.printf("%nFile '%s' ...%n", file);

         started = System.currentTimeMillis();
         time("file.exists()", file.exists());
         time("file.lastModified()>0", file.lastModified() > 0);
         time("Files.exists(path)", Files.exists(path));
         time("Files.readAttributes(path)", readAttributes(path) != null);
      }
   }

   private static void time(String msg, boolean exists) {
      System.out.printf("%-30s | %-5s | time=%s ms%n", msg, exists, System.currentTimeMillis() - started);
      started = System.currentTimeMillis();
   }

   private static BasicFileAttributes readAttributes(Path path) {
      try {
         return Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
      }
      catch (IOException e) {
         return null;
      }
   }

   public static void main(String[] args) {
      exists(FILES);
   }
}

Output:

 File 'C:\Users\p2r'... file.exists() | true | time=1 ms file.lastModified()>=0 | true | time=0 ms Files.exists(path) | true | time=2 ms Files.readAttributes(path) | true | time=0 ms File '\no\such\file'... file.exists() | false | time=0 ms file.lastModified()>=0 | false | time=0 ms Files.exists(path) | false | time=1 ms Files.readAttributes(path) | false | time=0 ms File 'C:\no\such\file'... file.exists() | false | time=0 ms file.lastModified()>=0 | false | time=0 ms Files.exists(path) | false | time=1 ms Files.readAttributes(path) | false | time=1 ms File 'R:\'... file.exists() | false | time=0 ms file.lastModified()>=0 | false | time=0 ms Files.exists(path) | false | time=0 ms Files.readAttributes(path) | false | time=0 ms File 'S:\'... file.exists() | false | time=7802 ms file.lastModified()>=0 | false | time=10067 ms Files.exists(path) | false | time=7171 ms Files.readAttributes(path) | false | time=10047 ms

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM