简体   繁体   English

如何处理Java文本文件中的每个五个单词?

[英]How do I process each five words in a text file in Java?

Greetings All; 问候所有;

I have a text file say "test.txt" and I want to make process on each 5 words only. 我有一个说“ test.txt”的文本文件,我只想对每个5个单词进行处理。

for example if the test.txt contain: 例如,如果test.txt包含:

On the Insert tab the galleries include items that are designed to coordinate with the overall look of your document.

I want to take the first five words: On the Insert tab the , do some functions on them. 我想使用前五个词: On the Insert tab the ,对它们执行一些功能。 then the next five words galleries include items that are ,do functions...etc until the end of the file. 然后接下来的五个单词galleries include items that are执行功能...等的galleries include items that are直到文件末尾。

I want to do that with java.Any Ideas? 我想用java.Any想法吗?

So this pseudo code: 所以这个伪代码:

  • Read the file 读取文件
  • Put the words in a list 将单词放在列表中
  • while( remain unprocessed items ) while(保留未处理的项目)
    • Take five 拿五
    • processThem 处理他们
  • repeat 重复

Could be implemented along the lines. 可以沿线实施。

String fileContent = readFile("test.txt");
List<String> words = splitWordsIntoList( fileContent );
int n = 0;
List<String> five = new ArrayList<String>();
for( String word : words ) { 
  if( n++ < 5 ) { 
     five.add( word );
  } else { 
      n = 0 ;
      process( five );
  }
}

Check out the String.split() method in the SDK. 检出SDK中的String.split()方法。 Probably gets you a good ways where you're heading. 可能会为您提供前往目的地的好方法。

您可以将整个文本文件读取为单个字符串,并使用字符串标记器创建单词数组,只要您感兴趣的单词始终用空格分隔即可。

Word groups of 5, then loop over the found matches. 5个单词组,然后遍历找到的匹配项。

Pattern p = Pattern.compile("(\\w*\\s?){5}");
String s = "On the Insert tab the galleries include items that are designed to coordinate with the overall look of your document.";
Matcher m = p.matcher(s);
while (m.find()) {
   String words_group = m.group();
   System.out.println(words_group);
}

To split the words_group you can: 要拆分words_group,您可以:

words_group.split(" "); // returns String[]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java如何从文本文件中提取单词? - How Java extracts words from a text file? 如何在文本文件中拆分每个数据并将它们放回Java GUI表单上的各自文本字段中? - How do I split each piece of data in a text file and place them back in their respective text fields on a Java GUI form? 如何在文本文件中搜索以a结尾的单词,然后将r附加到这些单词? (Java) - How to search a text file for words ending with a and then append r to these words? (Java) 如何从我的代码java中的文件中计算每个段落中的单词数? - how can I count number of words from each paragraph from the file in my code java? 我在Java服务器中拥有五个线程,如何使它们同时运行一个任务? - I hava five threads in java server, how should I do to make them run one task concurrently? 如何使用Java计算文件中的每个单词 - How do I count each word in a file using Java 如何将单词从文本文件添加到我创建的数组中? - How do I add words from an text file to an array i created? 基于此代码Java的文件的每个句子中有多少个单词 - How many words are in each sentence of the file based in this code Java Java:如何读取文本文件的每一行并将每一行设置为数组元素? - Java: How do you read each line of a text file and setting each line as an array element? 当文件路径包含Linux中的中文单词时,如何检查Java中是否存在文件? - How do I check if a file exists in Java when the file path contains chinese words in Linux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM