Java Path File List nio listOfWords(String filePath)

Here you can find the source of listOfWords(String filePath)

Description

Reads the file from the given path

License

Open Source License

Parameter

Parameter Description
filePath relative/absolute filePath on disk

Exception

Parameter Description
FileNotFoundException if file is not found on disk

Return

Set of all the words in the file

Declaration

public static Set<String> listOfWords(String filePath) throws FileNotFoundException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileNotFoundException;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {
    private static Set<String> words = new HashSet<>();

    /**//from   w  ww .  j  a  v a 2s . c om
     * Reads the file from the given path
     * @param filePath relative/absolute filePath on disk
     * @return Set of all the words in the file
     * @throws FileNotFoundException if file is not found on disk
     */
    public static Set<String> listOfWords(String filePath) throws FileNotFoundException {
        String path = Paths.get(".").toAbsolutePath().normalize().toString();
        try (Scanner scanner = new Scanner(new File(path + "/" + filePath))) {
            while (scanner.hasNext()) {
                words.add(scanner.nextLine());
            }
            return words;
        }
    }
}

Related

  1. listFiles(Path path)
  2. listFiles(Path path, List preorderList)
  3. listFiles(Path path, String glob)
  4. listFilesRecursively(Path dir, final DirectoryStream.Filter filter, final boolean recursive)
  5. listFoldersInFolder2(String path)
  6. listSteps(Path scenarioDirectory)
  7. loadDataFilesList(String prefix, Path bwcIndicesPath)
  8. loadTrainingData(Path path, List input, List output, int outputCount)
  9. mergeFiles(List files, Path mergedFile)