2017-12-09 6 views
0

私は苦労しています。ファイルのディレクトリにアクセスしてファイル数をカウントした後、ファイルそのものを読み込み、各ファイル内の単語数をカウントする割り当てを終えようとしています。これは私が投稿した質問の続きですが、「答えは」これは問題の概要であるすべての(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ループに移動して成功することはありません。私が行をコメントアウトすると、ファイルカウンタは正常に動作します。私はこのプログラムを "歩く(カウントファイル)とガム(ファイル中の単語を数える)"にすることができます誰も知っている?ここで

+0

をあなたはおそらく、「ファイルという例外を取得しています: "が存在しない場合は、' Path path = Paths.get( ":"); 'をチェックしてください。 – Edwardth

+0

私はしましたが、実際のファイルEx:haiku.txtを入力しても同じことが起こります。 –

+0

mainでcatchする例外を 'ex.printStackTrace(); 'で出力します – Edwardth

答えて

0

あなたが作ったいくつかのミスです:ファイルが、それは全体のパスを渡す方が良いでしょうディレクトリにあるため、

  • count()にファイル名のみを渡します。
  • :のパスを使用しても、有効なファイル名ではありません。
  • スローされた例外を記録していないので、実際の問題は隠蔽されています。
  • ラムダを使用して、Javaランのほとんどとまだ闘争しているとき。

これは動作するはずです:

Main.class:

public class Main { 
    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)); 
      } 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
     System.out.println("Count: " + fileNames.size() + " files"); 
    } 
} 

WordReader.class:

public class WordReader { 
    public Map<String, Integer> count(Path filePath) throws IOException { 
     Map<String, Integer> wordMap = Files.lines(filePath) 
       .flatMap(line -> Arrays.stream(line.trim().split(" "))) 
       .map(word -> word.replaceAll("[^a-zA-Z]", "").toLowerCase().trim()) 
       .filter(word -> word.length() > 0) 
       .collect(Collectors.groupingBy(s->s, Collectors.counting())); 

     wordMap.forEach((k, v) -> System.out.println(String.format(k, v))); 
     return wordMap; 
    } 
} 
+0

になっているとしたら、おそらくは 'parallel'をファイル処理に追加しています。 – Eugene

+0

@Eugene' parallel'が削除されました。 – Edwardth

関連する問題