私は苦労しています。ファイルのディレクトリにアクセスしてファイル数をカウントした後、ファイルそのものを読み込み、各ファイル内の単語数をカウントする割り当てを終えようとしています。これは私が投稿した質問の続きですが、「答えは」これは問題の概要であるすべての(How to count words in a text file, java 8-style)ファイルセット内のファイル数と単語数をカウントする方法Java 8とStreamsを使用する
で私の問題を解決しませんでした:
にストリームを使用するプログラムを書きますさまざまな長さの単語をファイルセット(files.zip)に効率的に数えます。あなたの出力は次のようになります:(カウント数は目的を説明するためのものです)。
Count 11 files:
word length: 1 ==> 80
word length: 2 ==> 321
word length: 3 ==> 643
Instead, I got the following output:
primes.txt
Count: 1 files
これは私が書いたコードです。私は、ファイル」という名前のディレクトリ読み込みメインクラスである二つのクラスFileReaderを使用しました:。理論的には、ディレクトリ内の各ファイル内の単語を数える必要があり、
FileReader.java
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author
*/
public class FileReader {
public static void main(String args[]) {
List<String> fileNames = new ArrayList<>();
try {
DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get("files"));
int fileCounter = 0;
**WordReader wordCnt = new WordReader();**
for (Path path : directoryStream) {
System.out.println(path.getFileName());
fileCounter++;
fileNames.add(path.getFileName().toString());
**System.out.println("word length: " + fileCounter + " ==> "
+ wordCnt.count(path.getFileName().toString()));**
}
} catch (IOException ex) {
}
System.out.println("Count: " + fileNames.size() + " files");
}
}
そしてWordReaderクラスのクラスでラムダ構文で記述された:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.AbstractMap.SimpleEntry;
import java.util.Arrays;
import java.util.Map;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
/**
*
* @author
*/
public class WordReader {
/**
*
* @param filename
* @return
* @throws java.io.IOException
*/
public Map<String, Long> count(String filename) throws IOException {
//Stream<String> lines = Files.lines(Paths.get(filename));
Path path = Paths.get(":");
Map<String, Long> wordMap = Files.lines(path)
.parallel()
.flatMap(line -> Arrays.stream(line.trim().split(" ")))
.map(word -> word.replaceAll("[^a-zA-Z]", "").toLowerCase().trim())
.filter(word -> word.length() > 0)
.map(word -> new SimpleEntry<>(word, 1))
//.collect(Collectors.toMap(s -> s, s -> 1, Integer::sum));
.collect(groupingBy(SimpleEntry::getKey, counting()));
wordMap.forEach((k, v) -> System.out.println(String.format(k,v)));
return wordMap;
}
}
私はカウンターを停止(BOLDで強調表示)WordReaderクラスを呼び出すことで問題を持っていると信じていますが、私はそれを解決する方法がわからないと私が試してみましたクラス呼び出しをforループに移動して成功することはありません。私が行をコメントアウトすると、ファイルカウンタは正常に動作します。私はこのプログラムを "歩く(カウントファイル)とガム(ファイル中の単語を数える)"にすることができます誰も知っている?ここで
をあなたはおそらく、「ファイルという例外を取得しています: "が存在しない場合は、' Path path = Paths.get( ":"); 'をチェックしてください。 – Edwardth
私はしましたが、実際のファイルEx:haiku.txtを入力しても同じことが起こります。 –
mainでcatchする例外を 'ex.printStackTrace(); 'で出力します – Edwardth