2017-03-22 3 views
-1

Javaでファイルに文字列を書き込もうとしています。JavaファイルI/Oでの問題

基本的には、テキストを含むファイル(文字列と見なされます)から読み込むことになっています。各文字列に対して、文字列がword1 word2の形式(つまり2単語の場合)の場合は、1つのファイル(名前のリスト)に書き込む必要があります。一方、その形式を持たない場合は、 System.out(拡張子は.txt)と呼ばれる別のファイルに書き込む必要があります。

私は名前の部分がうまく動作しています。名前は出力ファイルに書き込まれますが、System.outファイルに書き込むときには1行だけが書き込まれます。

したがって、通常の文章(「これは書かれてはいけません」など)はスキップされます。なぜこのことが分かりませんか?ここに私のコードからいくつか抜粋です:

public static void processData(BufferedReader inputFile, PrintWriter outputFile) 
    { 

     // A modified version of 
     // the algorithm shown 
     // on the class page. 

     String inputLine = null; 

     try 
     { 
      // While loop that processes 
      // each line taken from the file. 
      // First it checks whether the 
      // line has the format word1 word2 

      while ((inputLine = inputFile.readLine()) != null) 
      {    

       // Splits the line into tokens, 
       // taking the delimiter to be 
       // anything that is not an 
       // upper or lowercase letter 
       // or the tab character (\t) 
       // in groups of at least one 
       // (using the regex [^a-zA-Z]+) 

       String[] tokens = inputLine.split("[^a-zA-Z\t]+"); 

       // REMOVE LINES 215-216 
       System.out.println("inputLine is: " + inputLine); 
       System.out.println("The length of tokens is: " + tokens.length); 

       // If the line has the format 
       // word1 word2, the outputWriter 
       // method is invoked to process 
       // the words and write them to 
       // the output file 

       if (tokens.length == 2){ 
        outputWriter(tokens, outputFile); 
       } 

       // Otherwise, the systemWriter 
       // method is invoked to write 
       // the line to the System.out file 

       // REMOVE LINE 234 
       else{ 
        System.out.println("ENTERED SYSTEMWRITER TREE AT: " + inputLine); 
        systemWriter(tokens); 
       } 
      } 
     } 

答えて

0

あなたはこのように、スペースで分割する必要があります

String[] tokens = inputLine.split(" "); 

これは私が何をしたいと思い単語の配列を、行います。