test/java/nio/file/DirectoryStream/DriveLetter.java

Print this page




  35 public class DriveLetter {
  36 
  37     public static void main(String[] args) throws IOException {
  38         String os = System.getProperty("os.name");
  39         if (!os.startsWith("Windows")) {
  40             System.out.println("This is Windows specific test");
  41             return;
  42         }
  43         String here = System.getProperty("user.dir");
  44         if (here.length() < 2 || here.charAt(1) != ':')
  45             throw new RuntimeException("Unable to determine drive letter");
  46 
  47         // create temporary file in current directory
  48         File tempFile = File.createTempFile("foo", "tmp", new File(here));
  49         try {
  50             // we should expect C:foo.tmp to be returned by iterator
  51             String drive = here.substring(0, 2);
  52             Path expected = Paths.get(drive).resolve(tempFile.getName());
  53 
  54             boolean found = false;
  55             DirectoryStream<Path> stream = Paths.get(drive).newDirectoryStream();
  56             try {
  57                 for (Path file : stream) {
  58                     if (file.equals(expected)) {
  59                         found = true;
  60                         break;
  61                     }
  62                 }
  63             } finally {
  64                 stream.close();
  65             }
  66             if (!found)
  67                 throw new RuntimeException("Temporary file not found???");
  68 
  69         } finally {
  70             tempFile.delete();
  71         }
  72     }
  73 }


  35 public class DriveLetter {
  36 
  37     public static void main(String[] args) throws IOException {
  38         String os = System.getProperty("os.name");
  39         if (!os.startsWith("Windows")) {
  40             System.out.println("This is Windows specific test");
  41             return;
  42         }
  43         String here = System.getProperty("user.dir");
  44         if (here.length() < 2 || here.charAt(1) != ':')
  45             throw new RuntimeException("Unable to determine drive letter");
  46 
  47         // create temporary file in current directory
  48         File tempFile = File.createTempFile("foo", "tmp", new File(here));
  49         try {
  50             // we should expect C:foo.tmp to be returned by iterator
  51             String drive = here.substring(0, 2);
  52             Path expected = Paths.get(drive).resolve(tempFile.getName());
  53 
  54             boolean found = false;
  55             Path dir = Paths.get(drive);
  56             try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
  57                 for (Path file : stream) {
  58                     if (file.equals(expected)) {
  59                         found = true;
  60                         break;
  61                     }
  62                 }


  63             }
  64             if (!found)
  65                 throw new RuntimeException("Temporary file not found???");
  66 
  67         } finally {
  68             tempFile.delete();
  69         }
  70     }
  71 }