-2

\t 複数見つかった場合は、必要なものに基づいてファイルデータを解析しています\t。次の新しい行から解析を開始し、0から配列リストの単語を開始します。

public static void readFile() throws IOException {
  String line;
  ArrayList<String> words = new ArrayList<String>();

  try {
    BufferedReader inFile = new BufferedReader (new FileReader ("/weather.txt"));

    while ((line = inFile.readLine()) != null) {
      StringTokenizer token= new StringTokenizer(line,"\t");
      while (token.hasMoreTokens()) {   
        words.add(token.nextToken());
      }

      /*
       * function to print the values
       */                   
      getMetadataTriple(words);
    }
  }
4

1 に答える 1

0

次の正規表現ソリューションを試してください。

public static void readFile() throws IOException
{
    String line;
    ArrayList<String> words = new ArrayList<String>();    
    try
    {
        BufferedReader inFile = new BufferedReader(new FileReader("weather.txt"));

        while ((line = inFile.readLine()) != null)
        {
            String[] temp = line.split("\\t");
            for (String s : temp)
            {
                if(!s.isEmpty())
                {
                    words.add(s);
                }
                else //another \t
                {
                    words.clear();
                    break;
                }   
            }

            /*
             * function to print the values
             */
            getMetadataTriple(words);
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

}
于 2012-08-16T11:56:55.483 に答える