StringTokenizer 从下一行开始,如果在文件 java 中有多个 (t)



我正在根据我想要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);
    }
  }

尝试以下正则表达式解决方案:

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();
    }
}

相关内容

  • 没有找到相关文章

最新更新