Example usage for java.nio.file SimpleFileVisitor SimpleFileVisitor

List of usage examples for java.nio.file SimpleFileVisitor SimpleFileVisitor

Introduction

In this page you can find the example usage for java.nio.file SimpleFileVisitor SimpleFileVisitor.

Prototype

protected SimpleFileVisitor() 

Source Link

Document

Initializes a new instance of this class.

Usage

From source file:net.librec.data.convertor.TextDataConvertor.java

/**
 * Read data from the data file. Note that we didn't take care of the
 * duplicated lines./*from   w  w w  . ja  va 2s .c o  m*/
 *
 * @param dataColumnFormat
 *            the format of input data file
 * @param inputDataPath
 *            the path of input data file
 * @param binThold
 *            the threshold to binarize a rating. If a rating is greater
 *            than the threshold, the value will be 1; otherwise 0. To
 *            disable this appender, i.e., keep the original rating value,
 *            set the threshold a negative value
 * @throws IOException
 *            if the <code>inputDataPath</code> is not valid.
 */
private void readData(String dataColumnFormat, String inputDataPath, double binThold) throws IOException {
    LOG.info(String.format("Dataset: %s", StringUtil.last(inputDataPath, 38)));
    // Table {row-id, col-id, rate}
    Table<Integer, Integer, Double> dataTable = HashBasedTable.create();
    // Table {row-id, col-id, timestamp}
    Table<Integer, Integer, Long> timeTable = null;
    // Map {col-id, multiple row-id}: used to fast build a rating matrix
    Multimap<Integer, Integer> colMap = HashMultimap.create();
    // BiMap {raw id, inner id} userIds, itemIds
    if (this.userIds == null) {
        this.userIds = HashBiMap.create();
    }
    if (this.itemIds == null) {
        this.itemIds = HashBiMap.create();
    }
    final List<File> files = new ArrayList<File>();
    final ArrayList<Long> fileSizeList = new ArrayList<Long>();
    SimpleFileVisitor<Path> finder = new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            fileSizeList.add(file.toFile().length());
            files.add(file.toFile());
            return super.visitFile(file, attrs);
        }
    };
    Files.walkFileTree(Paths.get(inputDataPath), finder);
    LOG.info("All dataset files " + files.toString());
    long allFileSize = 0;
    for (Long everyFileSize : fileSizeList) {
        allFileSize = allFileSize + everyFileSize.longValue();
    }
    LOG.info("All dataset files size " + Long.toString(allFileSize));
    int readingFileCount = 0;
    long loadAllFileByte = 0;
    // loop every dataFile collecting from walkFileTree
    for (File dataFile : files) {
        LOG.info("Now loading dataset file " + dataFile.toString().substring(
                dataFile.toString().lastIndexOf(File.separator) + 1, dataFile.toString().lastIndexOf(".")));
        readingFileCount += 1;
        loadFilePathRate = readingFileCount / (float) files.size();
        long readingOneFileByte = 0;
        FileInputStream fis = new FileInputStream(dataFile);
        FileChannel fileRead = fis.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
        int len;
        String bufferLine = new String();
        byte[] bytes = new byte[BSIZE];
        while ((len = fileRead.read(buffer)) != -1) {
            readingOneFileByte += len;
            loadDataFileRate = readingOneFileByte / (float) fileRead.size();
            loadAllFileByte += len;
            loadAllFileRate = loadAllFileByte / (float) allFileSize;
            buffer.flip();
            buffer.get(bytes, 0, len);
            bufferLine = bufferLine.concat(new String(bytes, 0, len));
            bufferLine = bufferLine.replaceAll("\r", "\n");
            String[] bufferData = bufferLine.split("(\n)+");
            boolean isComplete = bufferLine.endsWith("\n");
            int loopLength = isComplete ? bufferData.length : bufferData.length - 1;
            for (int i = 0; i < loopLength; i++) {
                String line = new String(bufferData[i]);
                String[] data = line.trim().split("[ \t,]+");
                String user = data[0];
                String item = data[1];
                Double rate = ((dataColumnFormat.equals("UIR") || dataColumnFormat.equals("UIRT"))
                        && data.length >= 3) ? Double.valueOf(data[2]) : 1.0;

                // binarize the rating for item recommendation task
                if (binThold >= 0) {
                    rate = rate > binThold ? 1.0 : 0.0;
                }

                // inner id starting from 0
                int row = userIds.containsKey(user) ? userIds.get(user) : userIds.size();
                userIds.put(user, row);

                int col = itemIds.containsKey(item) ? itemIds.get(item) : itemIds.size();
                itemIds.put(item, col);

                dataTable.put(row, col, rate);
                colMap.put(col, row);
                // record rating's issuing time
                if (StringUtils.equals(dataColumnFormat, "UIRT") && data.length >= 4) {
                    if (timeTable == null) {
                        timeTable = HashBasedTable.create();
                    }
                    // convert to million-seconds
                    long mms = 0L;
                    try {
                        mms = Long.parseLong(data[3]); // cannot format
                        // 9.7323480e+008
                    } catch (NumberFormatException e) {
                        mms = (long) Double.parseDouble(data[3]);
                    }
                    long timestamp = timeUnit.toMillis(mms);
                    timeTable.put(row, col, timestamp);
                }
            }
            if (!isComplete) {
                bufferLine = bufferData[bufferData.length - 1];
            }
            buffer.clear();
        }
        fileRead.close();
        fis.close();
    }
    int numRows = numUsers(), numCols = numItems();
    // build rating matrix
    preferenceMatrix = new SparseMatrix(numRows, numCols, dataTable, colMap);
    if (timeTable != null)
        datetimeMatrix = new SparseMatrix(numRows, numCols, timeTable, colMap);
    // release memory of data table
    dataTable = null;
    timeTable = null;
}

From source file:com.oneops.util.SearchSenderTest.java

private void emptyRetryDir() {
    Path directory = Paths.get(retryDir);
    try {//w  w w .j a v a 2 s .co  m
        Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.delete(dir);
                return CONTINUE;
            }
        });
        Files.createDirectories(directory);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.elasticsearch.plugins.PluginManagerIT.java

/** creates a plugin .zip and returns the url for testing */
private String createPlugin(final Path structure, String... properties) throws IOException {
    writeProperties(structure, properties);
    Path zip = createTempDir().resolve(structure.getFileName() + ".zip");
    try (ZipOutputStream stream = new ZipOutputStream(Files.newOutputStream(zip))) {
        Files.walkFileTree(structure, new SimpleFileVisitor<Path>() {
            @Override/*from w w  w  .  j  a va  2s  .co  m*/
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                stream.putNextEntry(new ZipEntry(structure.relativize(file).toString()));
                Files.copy(file, stream);
                return FileVisitResult.CONTINUE;
            }
        });
    }
    if (randomBoolean()) {
        writeSha1(zip, false);
    } else if (randomBoolean()) {
        writeMd5(zip, false);
    }
    return zip.toUri().toURL().toString();
}

From source file:org.testeditor.fixture.swt.SwtBotFixture.java

/**
 * //from w w  w . j  a  va 2s.  c  o  m
 * @return utility FileVisitor class for recursive directory delete
 *         operations.
 */
private FileVisitor<Path> getDeleteFileVisitor() {
    return new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            file.toFile().setWritable(true);
            Files.delete(file);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            Files.delete(dir);
            return FileVisitResult.CONTINUE;
        }
    };
}

From source file:org.talend.dataprep.cache.file.FileSystemContentCacheTest.java

@Test
public void testJanitorEvictionPeriod() throws Exception {

    // given some cache entries
    List<ContentCacheKey> keys = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        keys.add(new DummyCacheKey("janitor me " + i + 1));
    }//from   w  w w.ja va 2  s  .  com
    for (ContentCacheKey key : keys) {
        addCacheEntry(key, "janitor content", ContentCache.TimeToLive.IMMEDIATE);
        Assert.assertThat(cache.has(key), is(true));
    }

    // when eviction is performed and the janitor is called
    janitor.janitor();

    // then, none of the cache entries should be removed
    for (ContentCacheKey key : keys) {
        Assert.assertThat(cache.has(key), is(true));
    }

    Thread.sleep(ContentCache.TimeToLive.IMMEDIATE.getTime() + 500);

    // then, none of the cache entries should be removed
    for (ContentCacheKey key : keys) {
        Assert.assertThat(cache.has(key), is(false));
    }

    // when eviction is performed and the janitor is called
    janitor.janitor();
    Files.walkFileTree(Paths.get(TEST_DIRECTORY), new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (!StringUtils.contains(file.toFile().getName(), ".nfs")) {
                Assert.fail("file " + file + " was not cleaned by the janitor");
            }
            return super.visitFile(file, attrs);
        }
    });
}

From source file:org.apache.beam.sdk.io.TextIOTest.java

License:asdf

@AfterClass
public static void teardownClass() throws IOException {
    Files.walkFileTree(tempFolder, new SimpleFileVisitor<Path>() {
        @Override/*from www  .ja va  2  s. c o  m*/
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            Files.delete(file);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            Files.delete(dir);
            return FileVisitResult.CONTINUE;
        }
    });
}

From source file:org.elasticsearch.bootstrap.JarHell.java

/**
 * Checks the set of URLs for duplicate classes
 * @throws IllegalStateException if jar hell was found
 *//*from   w ww  .  j  a v a  2s .co  m*/
@SuppressForbidden(reason = "needs JarFile for speed, just reading entries")
public static void checkJarHell(URL urls[]) throws Exception {
    ESLogger logger = Loggers.getLogger(JarHell.class);
    // we don't try to be sneaky and use deprecated/internal/not portable stuff
    // like sun.boot.class.path, and with jigsaw we don't yet have a way to get
    // a "list" at all. So just exclude any elements underneath the java home
    String javaHome = System.getProperty("java.home");
    logger.debug("java.home: {}", javaHome);
    final Map<String, Path> clazzes = new HashMap<>(32768);
    Set<Path> seenJars = new HashSet<>();
    for (final URL url : urls) {
        final Path path = PathUtils.get(url.toURI());
        // exclude system resources
        if (path.startsWith(javaHome)) {
            logger.debug("excluding system resource: {}", path);
            continue;
        }
        if (path.toString().endsWith(".jar")) {
            if (!seenJars.add(path)) {
                logger.debug("excluding duplicate classpath element: {}", path);
                continue; // we can't fail because of sheistiness with joda-time
            }
            logger.debug("examining jar: {}", path);
            try (JarFile file = new JarFile(path.toString())) {
                Manifest manifest = file.getManifest();
                if (manifest != null) {
                    checkManifest(manifest, path);
                }
                // inspect entries
                Enumeration<JarEntry> elements = file.entries();
                while (elements.hasMoreElements()) {
                    String entry = elements.nextElement().getName();
                    if (entry.endsWith(".class")) {
                        // for jar format, the separator is defined as /
                        entry = entry.replace('/', '.').substring(0, entry.length() - 6);
                        checkClass(clazzes, entry, path);
                    }
                }
            }
        } else {
            logger.debug("examining directory: {}", path);
            // case for tests: where we have class files in the classpath
            final Path root = PathUtils.get(url.toURI());
            final String sep = root.getFileSystem().getSeparator();
            Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    String entry = root.relativize(file).toString();
                    if (entry.endsWith(".class")) {
                        // normalize with the os separator
                        entry = entry.replace(sep, ".").substring(0, entry.length() - 6);
                        checkClass(clazzes, entry, path);
                    }
                    return super.visitFile(file, attrs);
                }
            });
        }
    }
}

From source file:org.batfish.common.plugin.PluginConsumer.java

protected final void loadPlugins() {
    for (Path pluginDir : _pluginDirs) {
        if (Files.exists(pluginDir)) {
            try {
                Files.walkFileTree(pluginDir, new SimpleFileVisitor<Path>() {
                    @Override// w  ww  .jav a2 s .c om
                    public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                        loadPluginJar(path);
                        return FileVisitResult.CONTINUE;
                    }
                });
            } catch (IOException e) {
                throw new BatfishException("Error walking through plugin dir: '" + pluginDir.toString() + "'",
                        e);
            }
        }
    }
}

From source file:com.ejisto.util.IOUtils.java

public static Collection<String> findAllClassesInJarFile(File jarFile) throws IOException {
    final List<String> ret = new ArrayList<>();
    Map<String, String> env = new HashMap<>();
    env.put("create", "false");
    try (FileSystem targetFs = FileSystems.newFileSystem(URI.create("jar:file:" + jarFile.getAbsolutePath()),
            env)) {//w ww.  ja  v a  2 s. co  m
        final Path root = targetFs.getPath("/");
        Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                String ext = ".class";
                if (attrs.isRegularFile() && file.getFileName().toString().endsWith(ext)) {
                    String path = root.relativize(file).toString();
                    ret.add(translatePath(path.substring(0, path.length() - ext.length()), "/"));
                }
                return FileVisitResult.CONTINUE;
            }
        });
    }
    return ret;
}

From source file:com.sastix.cms.server.services.content.HashedDirectoryServiceTest.java

private int listRecursive(Path path) throws IOException {
    final int[] counter = { 0 };
    Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
        @Override/*from   w w  w .  ja v  a 2s .  co  m*/
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            counter[0]++;
            LOGGER.trace("Listing: [" + counter[0] + "]\t" + file.getParent() + "/" + file.getFileName());
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            return FileVisitResult.CONTINUE;
        }
    });
    return counter[0];
}